package com.sqx.modules.errand.controller.app;

import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.PageUtils;
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.service.CommonInfoService;
import com.sqx.modules.errand.dto.*;
import com.sqx.modules.errand.entity.ErrandAddress;
import com.sqx.modules.errand.entity.ErrandComplaint;
import com.sqx.modules.errand.entity.ErrandEvaluate;
import com.sqx.modules.errand.entity.ErrandRedPacket;
import com.sqx.modules.errand.entity.TbIndent;
import com.sqx.modules.errand.service.ErrandComplaintService;
import com.sqx.modules.errand.service.TbIndentService;
import icu.xuyijie.secureapi.annotation.DecryptParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/tbindent")
@Api(value = "订单", tags = {"订单"})
public class AppTbIndentController {

    @Autowired
    private TbIndentService tbIndentService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private ErrandComplaintService errandComplaintService;

    @Login
    @PostMapping(value = "distance")
    @ApiOperation("根据经纬度计算两点之间距离")
    public Result distance(double ol, double od, double dl, double dd) {

        return tbIndentService.distance(ol, od, dl, dd);
    }

    @Login
    @ApiOperation("保存用户输入地址")
    @PostMapping(value = "addUserAddress")
    public Result addUserAddress(@RequestAttribute Long userId, @RequestBody ErrandAddress errandAddress) {

        return tbIndentService.addUserAddress(userId, errandAddress);
    }

    @Login
    @ApiOperation("编辑地址")
    @PostMapping(value = "updateAddress")
    public Result updateAddress(@RequestAttribute Long userId, @RequestBody ErrandAddress errandAddress) {

        return tbIndentService.updateAddress(userId, errandAddress);
    }

    @Login
    @ApiOperation("获取用户历史地址")
    @GetMapping(value = "findUserAddress")
    public Result findUserAddress(@RequestAttribute Long userId) {

        return tbIndentService.findUserAddress(userId);
    }

    @Login
    @ApiOperation("根据地址id获取地址信息")
    @GetMapping(value = "findAddressById")
    public Result findAddressById(@RequestAttribute Long userId, Integer addressId) {

        return tbIndentService.findAddressById(userId, addressId);
    }

    @Login
    @ApiOperation("设置默认地址")
    @GetMapping(value = "addDefaultAddress")
    private Result addDefaultAddress(@RequestAttribute Long userId, Integer addressId) {

        return tbIndentService.addDefaultAddress(userId, addressId);
    }

    @Login
    @ApiOperation("删除历史地址")
    @GetMapping(value = "delUserAddress")
    public Result delUserAddress(@RequestAttribute Long userId, Integer addressId) {

        return tbIndentService.delUserAddress(userId, addressId);
    }

    @Login
    @ApiOperation("获取订单起点附近5公里内所有骑手")
    @GetMapping(value = "find5KmRider")
    public Result find5KmRider(double lng, double lat) {

        return tbIndentService.find5KmRider(lng, lat);
    }

    @Login
    @ApiOperation("骑手上线时更新骑手坐标")
    @PostMapping(value = "updateCoordinate")
    public Result updateCoordinate(@RequestAttribute Long userId, double lng, double lat) {

        return tbIndentService.updateCoordinate(userId, lng, lat);
    }


    @Login
    @GetMapping(value = "/findUserInfoById")
    @ApiOperation("获取登陆用户信息")
    public Result findUserInfoById(@RequestAttribute("userId") Long userId) {
        UserEntity userEntity = tbIndentService.findUserInfoById(userId);

        return Result.success().put("data", userEntity);
    }

    @Login
    @ApiOperation("新用户领取新人红包")
    @GetMapping(value = "getNewUserRedPacket")
    public Result getNewUserRedPacket(@RequestAttribute Long userId) {

        return tbIndentService.getNewUserRedPacket(userId);
    }

    @Login
    @GetMapping(value = "findAllRedPacket")
    @ApiOperation("获取该用户的所有红包")
    public Result findAllRedPacket(@RequestAttribute Long userId) {
        List<ErrandRedPacket> allRedPacket = tbIndentService.findAllRedPacket(userId);
        return Result.success().put("data", allRedPacket);
    }

    @Login
    @PostMapping(value = "findRedPacket")
    @ApiOperation("获取用户可用红包")
    public Result findRedPacket(@RequestAttribute Long userId, @RequestBody TbIndent indent) {
        List<ErrandRedPacket> redPackets = tbIndentService.findRedPacket(userId, indent);
        return Result.success().put("data", redPackets);
    }

    @Login
    @PostMapping(value = "basicsMoney")
    @ApiOperation("计算订单基础价格")
    public Result basicsMoney(@RequestBody TbIndent tbIndent) {

        return Result.success().put("data", tbIndentService.basicsMoney(tbIndent));
    }

    @Login
    @PostMapping(value = "addIndent")
    @ApiOperation("用户下单")
    public Result addIndentBuy(@RequestAttribute Long userId, @RequestBody TbIndent tbIndent) {
        tbIndent.setUserId(userId);
        return tbIndentService.addIndentBuy(tbIndent);
    }

    @Login
    @PostMapping(value = "findNewIndent")
    @ApiOperation("骑手查看可接订单")
    public Result findNewIndent(@RequestAttribute Long userId, @RequestBody TbIndent tbIndent) {
        String receivingPettern = tbIndent.getReceivingPettern();
        double riderlat = tbIndent.getRiderlat();
        double riderlng = tbIndent.getRiderlng();
        Integer page = tbIndent.getPage();
        Integer limit = tbIndent.getLimit();
        String indentType = tbIndent.getIndentType();
        String buyType = tbIndent.getBuyType();
        return tbIndentService.findNewIndent(userId, receivingPettern, page, limit, riderlng, riderlat, indentType, buyType);
    }

    @Login
    @PostMapping(value = "find-new-indent")
    @ApiOperation("骑手查看可接订单(新)")
    public Result findNewIndent(@Valid @RequestBody WaitForAcceptOrderQueryDTO queryDTO) {
        PageUtils newIndent = tbIndentService.findNewIndent(queryDTO);
        return Result.success().put("data", newIndent);
    }

    @Login
    @PostMapping(value = "find-new-indent-group-by-shop")
    @ApiOperation("骑手查看可接订单(按商家分组)")
    public Result findNewIndentGroupByShop(@Valid @RequestBody WaitForAcceptOrderQueryDTO queryDTO) {
        PageUtils newIndent = tbIndentService.findNewIndentGroupByShop(queryDTO);
        return Result.success().put("data", newIndent);
    }

    @Login
    @PostMapping(value = "indentReceiving")
    @ApiOperation("骑手接单")
    public Result indentReceiving(@RequestAttribute Long userId, String indentNumber) {

        return tbIndentService.indentReceiving(userId, indentNumber);
    }

    @Login
    @PostMapping(value = "riderBuyGoods")
    @ApiOperation("骑手修改订单状态")
    public Result riderBuyGoods(@RequestBody TbIndent tbIndent) {

        return tbIndentService.riderBuyGoods(tbIndent);
    }

    @Login
    @GetMapping(value = "findUserIndent")
    @ApiOperation("用户查看自己所有订单（全部、待付款、待接单、派送中（已接单未完成）、已完成）")
    public Result findUserIndent(@RequestAttribute Long userId, Integer page, Integer limit, String indentState) {

        return tbIndentService.findUserIndent(userId, page, limit, indentState);
    }


    @PostMapping(value = "userIndentMessage")
    @ApiOperation("用户查看订单详情")
    public Result userIndentMessage(String indentNumber) {
        TbIndent indent = tbIndentService.userIndentMessage(indentNumber);
        return Result.success().put("data", indent);
    }

    @Login
    @PostMapping(value = "userCancleIndent")
    @ApiOperation("用户取消订单")
    public Result userCancleIndent(@RequestAttribute Long userId, String indentNumber) {

        return tbIndentService.userCancleIndent(userId, indentNumber);
    }

    @Login
    @GetMapping(value = "findRiderIndent")
    @ApiOperation("骑手查看自己所有订单")
    public Result findRiderIndent(@RequestAttribute Long userId, Integer page, Integer limit, String indentState) {

        return tbIndentService.findRiderIndent(userId, page, limit, indentState);
    }

    @Login
    @PostMapping(value = "indentMessage")
    @ApiOperation("骑手查看订单详情")
    public Result indentMessage(String indentNumber, double ol, double od) {
        TbIndent tbIndent = tbIndentService.indentMessage(indentNumber, ol, od);
        return Result.success().put("data", tbIndent);
    }

    @PostMapping(value = "indentMessageByOrderId")
    @ApiOperation("骑手查看订单详情")
    public Result indentMessageByOrderId(Long orderId, double ol, double od) {
        TbIndent tbIndent = tbIndentService.indentMessageByOrderId(orderId, ol, od);
        return Result.success().put("data", tbIndent);
    }

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

    //骑手已送达
    @Login
    @PostMapping(value = "riderDelivery")
    @ApiOperation("骑手已送达")
    public Result riderDelivery(@RequestAttribute Long userId, @Valid @RequestBody RiderDeliveryDTO riderDeliveryDTO) {

        return tbIndentService.riderDelivery(userId, riderDeliveryDTO);
    }

    @Login
    @PutMapping(value = "delivery-img/{indentNumber}")
    @ApiOperation("骑手上传送达照片")
    public Result riderUploadDeliveryImg(@RequestAttribute Long userId, @PathVariable @ApiParam("跑腿订单id") String indentNumber, @DecryptParam @ApiParam("图片地址") String imgs){
        tbIndentService.riderUploadDeliveryImg(userId, indentNumber, imgs);
        return Result.success();
    }

    //用户确认已送达
    @Login
    @PostMapping(value = "userDelivery")
    @ApiOperation("用户确认已送达")
    public Result userDelivery(@RequestAttribute Long userId, String indentNumber) {

        return tbIndentService.userDelivery(userId, indentNumber);
    }

    @Login
    @PostMapping(value = "userEvaluate")
    @ApiOperation("用户评价（评价等级和内容）")
    public Result userEvaluate(@RequestAttribute Long userId, @RequestBody ErrandEvaluate errandEvaluate) {
        errandEvaluate.setUserId(userId);
        return tbIndentService.userEvaluate(errandEvaluate);
    }

    @ApiOperation("用户投诉订单")
    @PostMapping(value = "insertComplaint")
    public Result insertComplaint(@RequestBody ErrandComplaint errandComplaint) {

        return errandComplaintService.insertComplaint(errandComplaint);
    }

    @Login
    @ApiOperation("用户查看所有投诉")
    @GetMapping(value = "selectComplaint")
    public Result selectComplaint(@RequestAttribute Long userId, Integer page, Integer limit) {

        return errandComplaintService.selectComplaint(userId, page, limit);
    }

    @Login
    @GetMapping(value = "findAllComplaint")
    @ApiOperation("骑手查看自己的所有被投诉")
    public Result findAllComplaint(@RequestAttribute Long userId, Integer illegalId, Integer complaintState, Integer page, Integer limit) {

        return errandComplaintService.findAllComplaint(userId, illegalId, complaintState, page, limit);
    }

    @Login
    @GetMapping(value = "findComplaint")
    @ApiOperation("骑手查看被投诉内容")
    public Result findComplaint(@RequestAttribute Long userId, Long complaintId) {

        return errandComplaintService.findComplaint(userId, complaintId);
    }

    @Login
    @PostMapping(value = "appealIndent")
    @ApiOperation("骑手提交申诉")
    public Result appealIndent(@RequestAttribute Long userId, @RequestBody ErrandComplaint errandComplaint) {

        return errandComplaintService.appealIndent(userId, errandComplaint);
    }

    @Autowired
    private UserService userService;


    @Login
    @ApiOperation("转单")
    @PostMapping("/transferOfOrder")
    public Result transferOfOrder(@RequestAttribute Long userId, @Valid RiderTransferOrderDTO transferOrderDTO) {
        // 检查入参
        checkTransferOrderParam(transferOrderDTO);

        tbIndentService.transferOfOrder(userId, transferOrderDTO);

        return Result.success();
    }

    @ApiOperation("获取骑手工作时间段及时效信息")
    @GetMapping("work-time")
    public Result getWorkTime() {
        String workingHoursOfRider = commonInfoService.findOne(422).getValue();
        String timeOfRider = commonInfoService.findOne(423).getValue();

        Map<String, String> data = new HashMap<>();
        data.put("workingHoursOfRider", workingHoursOfRider);
        data.put("timeOfRider", timeOfRider);

        return Result.success().put("data", data);
    }

    private void checkTransferOrderParam(RiderTransferOrderDTO transferOrderDTO) {
        if (StringUtils.isBlank(transferOrderDTO.getPhone()) && StringUtils.isBlank(transferOrderDTO.getRealName())) {
            throw new SqxException("待分配骑手手机号和待分配骑手真实姓名不能同时为空");
        }
    }


    //region 2024-09-12 A-jax 添加接口
    //region 1、根据订单id查询跑腿订单的信息；
    @Login
    @GetMapping("selectTbIndentPage")
    @ApiOperation("扫码或收单码查询跑腿订单列表（带分页")
    public Result selectMemberPage(@RequestAttribute Long userId, Integer page, Integer limit, String acquireCode) {
        //如果只有一条数据就看看数据是否能被当前骑手接
        PageUtils datas = tbIndentService.selectIndentPage(page, limit, acquireCode, userId);
        if (datas.getTotalCount() == 1) {
            //校验当前骑手是否能接单
            Result checkData = tbIndentService.checkIndent(userId, acquireCode);
            if((int)checkData.get("code") != 0){
                return checkData;//Result.error("当前订单无法接单");
            }
        }

        return Result.success().put("data",datas);
    }
    //endregion

    //region 2、根据订单id抢单（跑腿订单） 以及取货按钮功能；
    @Login
    @PostMapping(value = "orderIndentReceiving")
    @ApiOperation("骑手根据扫码或收单码接单")
    public Result orderIndentReceiving(@RequestAttribute Long userId, String acquireCode) {

        return tbIndentService.orderIndentReceiving(userId, acquireCode);
    }
    //endregion

    //region 3、根据订单id完成跑腿订单。
    @Login
    @PostMapping(value = "orderRiderDelivery")
    @ApiOperation("骑手根据扫码或收单码完成订单")
    public Result orderRiderDelivery(@RequestAttribute Long userId, @Valid @RequestBody OrderRiderDeliveryDTO orderRiderDeliveryDTO) {

        return tbIndentService.orderRiderDelivery(userId, orderRiderDeliveryDTO);
    }
    //endregion

    //region 转单
    @Login
    @ApiOperation("根据订单ID进行转单操作")
    @PostMapping("transferByOrderId")
    public Result transferByOrderId(@RequestAttribute Long userId, @Valid RiderTransferByOrderIdDTO transferByOrderIdDTO) {
        // 检查入参
        checkTransferByOrderIdParam(transferByOrderIdDTO);

        tbIndentService.transferByOrderId(userId, transferByOrderIdDTO);

        return Result.success();
    }

    private void checkTransferByOrderIdParam(RiderTransferByOrderIdDTO transferOrderDTO) {
        if (StringUtils.isBlank(transferOrderDTO.getPhone()) && StringUtils.isBlank(transferOrderDTO.getRealName())) {
            throw new SqxException("待分配骑手手机号和待分配骑手真实姓名不能同时为空");
        }
    }
    //endregion
    //endregion

}
