package com.yc.ship.module.trade.controller.app.pda;


import cn.hutool.core.util.StrUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.service.channel.ScenicChannelService;
import com.yc.ship.module.resource.service.machine.ScenicMachineService;
import com.yc.ship.module.resource.service.site.ScenicSiteService;
import com.yc.ship.module.trade.controller.admin.order.vo.check.TradeCheckPageReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.check.TradeCheckReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.check.TradeCheckRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderPdaRespVO;
import com.yc.ship.module.trade.controller.app.pda.vo.*;
import com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO;
import com.yc.ship.module.trade.enums.CheckTypeEnum;
import com.yc.ship.module.trade.framework.common.IPUtils;
import com.yc.ship.module.trade.service.order.TradeCheckService;
import com.yc.ship.module.trade.service.otc.OtcTradeOrderService;
import com.yc.ship.module.trade.service.pda.PdaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.error;
import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.CHECK;
import static com.yc.ship.module.trade.enums.ErrorCodeConstants.IDCARD_VOUCHER_CANNOT_TOGETHER_NULL;
import static com.yc.ship.module.trade.enums.ErrorCodeConstants.ORDER_CANNOT_TOGETHER_NULL;


/**
 * @author le'yang
 * &#064;Description ：PDA服务controller,
 */
@Tag(name = "PDA服务")
@RestController
@RequestMapping("/pda")
@Validated
@Slf4j
public class PdaController {

    @Resource
    private PdaService pdaService;
    @Resource
    private TradeCheckService tradeCheckService;
    @Resource
    private OtcTradeOrderService otcTradeOrderService;
    @Resource
    private ScenicSiteService scenicSiteService;
    @Resource
    private ScenicChannelService scenicChannelService;
    @Resource
    private ScenicMachineService scenicMachineService;


    @GetMapping("/get-machine-info")
    @Operation(summary = "获取检票通道")
    @OperateLog(type = API,enable = false)
    public CommonResult<?> getMachineInfo(){
        String machineId = IPUtils.getIpAddr();
        return success(scenicMachineService.getMachineInfo(1,machineId));
    }


    @GetMapping("/get-site-channel")
    @Operation(summary = "获取检票站点通道树")
    @OperateLog(type = API,enable = false)
    public CommonResult<?> getSiteChannel(HttpServletRequest request){
        request.getHeader("ClientId");
        return success(scenicChannelService.getCheckChannelList());

    }

    @PostMapping("/change-site-channel")
    @Operation(summary = "切换检票通道站点")
    @OperateLog(type = API)
    public CommonResult<?> changeCheckSiteChannel( @Valid @RequestBody PdaSiteChannelReqVO reqVO){
        reqVO.setMachineType(3);
        if (StrUtil.isBlank(reqVO.getMachineId())){
            reqVO.setMachineId(IPUtils.getIpAddr());
        }
        return success(scenicMachineService.changeCheckSiteChannel(reqVO.getMachineType(),reqVO.getChannelId(),reqVO.getMachineId()));
    }

    @PostMapping("/change-site-turnstile")
    @Operation(summary = "切换检票通道站点")
    @OperateLog(type = API)
    public CommonResult<?> changeCheckturnSiteChannel( @Valid @RequestBody PdaSiteChannelReqVO reqVO){
        reqVO.setMachineType(2);
        if (StrUtil.isBlank(reqVO.getMachineId())){
            reqVO.setMachineId(IPUtils.getIpAddr());
        }
        return success(scenicMachineService.changeCheckSiteChannel(reqVO.getMachineType(),reqVO.getChannelId(),reqVO.getMachineId()));
    }


    @PostMapping("/query-voucher-detail")
    @Operation(summary = "查询票券")
    @OperateLog(type = API)
    public CommonResult<?> queryVoucher( @Valid @RequestBody PdaQueryVoucherReqVO reqVO){
        if (StringUtils.isBlank(reqVO.getIdCard()) && StringUtils.isBlank(reqVO.getVoucherCode())) {
            return error(IDCARD_VOUCHER_CANNOT_TOGETHER_NULL);
        }
        return pdaService.queryVoucher(reqVO);
    }

    @PostMapping("/query-order-info")
    @Operation(summary = "查询订单信息")
    @OperateLog(type = API)
    public CommonResult<?> queryOrderInfo( @Valid @RequestBody PdaQueryOrderReqVO reqVO){
        if (StringUtils.isBlank(reqVO.getOrderNo()) && StringUtils.isBlank(reqVO.getPhone())&& StringUtils.isBlank(reqVO.getCardNo())) {
            return error(ORDER_CANNOT_TOGETHER_NULL);
        }
        return pdaService.queryOrder(reqVO);
    }

    @GetMapping("/orderInfo")
    @OperateLog(type = API)
    @Operation(summary = "查看订单详情", description = "根据订单id查询订单详情")
    public CommonResult<TradeOrderPdaRespVO> getOrderInfo(@Valid @RequestParam("id") Long id) {
        return success(otcTradeOrderService.getPdaOrderInfo(id));
    }

    @GetMapping("/orderDetailInfo")
    @Operation(summary = "查看门票详情", description = "根据订单id查询门票详情")
    @OperateLog(type = API)
    public CommonResult<List<TradeDetailBaseDO>> getDetailInfo(@Valid @RequestParam("id") Long id) {
        return success(otcTradeOrderService.getDetailInfo(id));
    }

    @PostMapping("/query-check-page")
    @Operation(summary = "分页查询检票记录")
    @OperateLog(type = API)
    public CommonResult<?> queryCheckPage( @Valid @RequestBody TradeCheckPageReqVO reqVO){

        return tradeCheckService.queryCheckPage(reqVO);
    }


    @PostMapping("check")
    @Operation(summary = "PDA核销")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> pdaCheck(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        if (StrUtil.isBlank(checkReqVO.getMachineId())){
            checkReqVO.setMachineId(IPUtils.getIpAddr());
        }
        checkReqVO.setType(CheckTypeEnum.PDA.getType());
        return tradeCheckService.checkPda(checkReqVO);
    }

    @PostMapping("pdaSingleCheck")
    @Operation(summary = "PDA单资源核销")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> pdaSingleCheck(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        if (StrUtil.isBlank(checkReqVO.getMachineId())){
            checkReqVO.setMachineId(IPUtils.getIpAddr());
        }
        checkReqVO.setType(CheckTypeEnum.Single.getType());
        return tradeCheckService.checkPda(checkReqVO);
    }

    @PostMapping("pdaCheckStatic")
    @Operation(summary = "PDA核销统计")
    @OperateLog(type = CHECK)
    public CommonResult<List<PdaCheckStaticRespVO>> pdaCheckStatic(@Valid @RequestBody PdaCheckStaticReqVO checkReqVO) {
        return tradeCheckService.pdaCheckStatic(checkReqVO);
    }

    @PostMapping("pdaCheckTotalStatic")
    @Operation(summary = "PDA核销统计汇总-按游船")
    @OperateLog(type = CHECK)
    public CommonResult<List<PdaCheckTotalStaticRespVO>> pdaCheckTotalStatic(@Valid @RequestBody PdaCheckStaticReqVO checkReqVO) {
        return tradeCheckService.pdaCheckTotalStatic(checkReqVO);
    }

    @PostMapping("pdaCheckTotalCount")
    @Operation(summary = "PDA核销统计汇总-按计调单查询当前总数")
    @OperateLog(type = CHECK)
    public CommonResult<Map<String,Object>> pdaCheckTotalCount(@Valid @RequestParam("dispatchId") String dispatchId) {
        return tradeCheckService.pdaCheckTotalCount(dispatchId);
    }

}
