package com.sqx.modules.errand.controller;

import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.errand.entity.TbIndent;
import com.sqx.modules.errand.service.TbIndentService;
import com.sqx.modules.order.entity.TbOrder;
import com.sqx.modules.order.service.AppOrderService;
import com.sqx.modules.utils.SenInfoCheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/tbindent")
@Api(value = "管理端-跑腿订单", tags = {"管理端-跑腿订单"})
public class TbIndentController {

    @Autowired
    private TbIndentService tbIndentService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private AppOrderService appOrderService;

    @ApiOperation("查看所有派单任务")
    @GetMapping(value = "findallIndent")
    public Result findallIndent(Integer page, Integer limit, Long userId, Long riderUserId, Long shopId,Integer indentType,String ordersNo,Integer status,String riderPhone){

        return tbIndentService.findallIndent(page, limit, userId, riderUserId, shopId,indentType,ordersNo,status,riderPhone);
    }

    @ApiOperation("任务中心 查看所有订单")
    @PostMapping(value = "findAllIndent")
    public Result findAllIndent(String indentType, String phone, String indentState, Integer page, Integer limit, String date, String dateType,String riderPhone){

        return tbIndentService.findAllIndent(indentType, phone, indentState, page, limit, date, dateType,riderPhone);
    }

    @ApiOperation("任务中心 查看所有订单")
    @PostMapping(value = "findAllIndents")
    public Result findAllIndents(String indentType, String phone, String indentState, Integer page, Integer limit, String date, String dateType,String riderPhone){

        return tbIndentService.findAllIndents(indentType, phone, indentState, page, limit, date, dateType,riderPhone);
    }

    @ApiOperation("订单中心")
    @PostMapping(value = "IndentCenter")
    public Result IndentCenter(Integer page, Integer limit, String indentType, String indentNumber, String indentState, String phone, String date, String dateType){

        return tbIndentService.IndentCenter(page, limit, indentType, indentNumber, indentState, phone, date, dateType);
    }

    @ApiOperation("管理端修改订单状态")
    @PostMapping(value = "updateIndentState")
    public Result updateIndentState(String indentNumber, String indentState){

        return tbIndentService.updateIndentState(indentNumber, indentState);
    }

    @ApiOperation("管理员下架订单")
    @PostMapping(value = "adminSoldOutIndent")
    public Result adminSoldOutIndent(String indentNumber){

        return tbIndentService.adminSoldOutIndent(indentNumber);
    }


    @PostMapping(value = "riderCancleIndent")
    @ApiOperation("骑手取消订单")
    public Result riderCancleIndent(String indentNumber){
        return tbIndentService.riderCancleIndent(indentNumber,2);
    }

    @ApiOperation("数据中心")
    @GetMapping(value = "findDataCenter")
    public Result findDataCenter(){

        return tbIndentService.findDataCenter();
    }

    @ApiOperation("任务分析")
    @GetMapping(value = "taskAnalysis")
    public Result taskAnalysis(String dateType, String date, Integer page, Integer limit, String indentState){

        return tbIndentService.taskAnalysis(dateType, date, page, limit, indentState);
    }

    @ApiOperation("订单分析")
    @GetMapping(value = "selectOrderByStatus")
    public Result selectOrderByStatus(String date, String dateType){

        return tbIndentService.selectOrderByStatus(date, dateType);
    }

    @ApiOperation("骑手收入统计 完成订单数统计")
    @GetMapping(value = "findIncome")
    public Result findIncome(Long userId, String date, String dateType, Integer page, Integer limit){

        return tbIndentService.findIncome(userId, date, dateType, page, limit);
    }


    @ApiOperation("转单")
    @PostMapping("/transferOfOrder")
    public Result transferOfOrder(Long indentId,Long riderUserId){
        TbIndent indent = tbIndentService.getById(indentId);
        if("5".equals(indent.getIndentType())){
            TbOrder tbOrder = appOrderService.getById(indent.getOrderId());
            if(tbOrder.getAutoSendOrder()!=null && tbOrder.getAutoSendOrder()==1){
                return Result.error("商家配送不允许转单！");
            }
        }

        UserEntity userEntity = userService.selectUserById(riderUserId);
        CommonInfo one1 = commonInfoService.findOne(273);
        Double cashDeposit = Double.parseDouble(one1.getValue());
        if(userEntity.getCashDeposit().doubleValue()<cashDeposit){
            return Result.error("骑手保证金不足！");
        }
        if(!"1".equals(userEntity.getCheckCertification())){
            return Result.error("骑手未进行实名认证！");
        }

        if(indent.getRiderUserId()!=null && userEntity.getUserId().equals(indent.getRiderUserId())){
            return Result.error("不能选择相同的骑手！");
        }
        indent.setRiderUserId(riderUserId);
        indent.setIsRider(1);
        tbIndentService.updateById(indent);
        UserEntity user = userService.getById(indent.getUserId());
        CommonInfo one = commonInfoService.findOne(335);
        List<String> msgList=new ArrayList<>();
        msgList.add(0,indent.getIndentNumber());
        if("3".equals(indent.getIndentState())){
            msgList.add(1,"已接单");
        }else if("4".equals(indent.getIndentState())){
            msgList.add(1,"已取货");
        }else{
            msgList.add(1,"进行中");
        }
        msgList.add(2,"骑手已更换，新骑手将尽快为您送达");
        msgList.add(3, DateUtils.format(new Date()));
        if(StringUtils.isNotEmpty(user.getOpenId())){
            SenInfoCheckUtil.sendMsg(user.getOpenId(),one.getValue(),msgList,5);
        }
        if(StringUtils.isNotEmpty(user.getClientid())){
            userService.pushToSingle("更换骑手","骑手已更换，新骑手将尽快为您送达",user.getClientid());
        }
        return Result.success();
    }


}
