package com.pj.tb_order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.pj.api.dto.HtDeclareChargebackDto;
import com.pj.api.dto.HtPassCardDTO;
import com.pj.api.dto.OrderDto;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import cn.dev33.satoken.annotation.SaCheckPermission;


/**
 * Controller: tb_order -- 订单表
 *
 * @author loovi
 */
@RestController
@RequestMapping("/TbOrder/")
public class TbOrderController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbOrderService tbOrderService;



    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_EDIT)
    public AjaxJson update(TbOrder t) {
        tbOrderService.update(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson getById(Long id) {
        TbOrder t = tbOrderService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrder> list = tbOrderService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }


    /**
     * 改 - 删除状态(0=禁用,1=启用)
     */
    @RequestMapping("updateDeleteStatus")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_EDIT)
    public AjaxJson updateDeleteStatus(Long id, Integer value) {
        int line = SP.publicMapper.updateColumnById(TbOrder.TABLE_NAME, "delete_status", value, id);
        return AjaxJson.getByLine(line);
    }


    /**
     * 改 - 删除状态(0=禁用,1=启用)
     */
    @RequestMapping("editUpPrice")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_EDIT_PRICE)
    public AjaxJson editUpPrice(Long id, double price) {
        int line = SP.publicMapper.updateColumnById(TbOrder.TABLE_NAME, "up_price", price, id);
        return AjaxJson.getByLine(line);
    }

    /**
     * 批量修改总价
     */
    @RequestMapping("editUpPriceByIds")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_EDIT_PRICE)
    public AjaxJson editUpPriceByIds(String ids, double price) {
        Long[] idArr = Convert.toLongArray(ids);

        List<TbOrder> tbOrders = new ArrayList<>();
        for (Long id : idArr) {
            TbOrder order = new TbOrder();
            order.setUpPrice(BigDecimal.valueOf(price));
            order.setId(id);
            tbOrders.add(order);
        }
        boolean batch = tbOrderService.updateBatchById(tbOrders);
        return AjaxJson.toAjax(batch);
    }

    /**
     * 订单补扣款
     */
    @RequestMapping("deduction")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_DEDUCTION)
    public AjaxJson deduction(Long id) {
        tbOrderService.deduction(id);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("orderRefund")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_REFUND)
    public AjaxJson orderRefund(@RequestParam("id")Long id) {
        return AjaxJson.toAjax(tbOrderService.orderRefund(id));
    }

    /**
     * 撤销转售
     */
    @RequestMapping("cancelSale")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson cancelSale(Long id) {
        tbOrderService.cancelSale(id);
        return AjaxJson.getSuccess();
    }
    /**
     * 撤销转售
     */
    @RequestMapping("cancelSaleBatch")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson cancelSaleBatch() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        tbOrderService.cancelSaleBatch(ids);
        return AjaxJson.getSuccess();
    }

    /**
     * 订单补扣款——按车，并且未扣款
     */
    @RequestMapping("deductionByVeNo")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_DEDUCTION)
    public AjaxJson deductionByVeNo(String veNo, Long id) {
        tbOrderService.deductionByVeNo(veNo, id);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("getDeductionByVeNoList")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson getDeductionByVeNoList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrder> list = tbOrderService.getDeductionByVeNoList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 订单补扣款
     */
    @RequestMapping("staticsStatus")
    public AjaxJson staticsStatus(Long saleMainId) {
        Map<String, Object> result = tbOrderService.staticsStatus(saleMainId);
        return AjaxJson.getSuccessData(result);
    }

    /**
     * 订单补发送009信息到航通
     */
    @RequestMapping("send009ToHt")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_SEND009)
    public AjaxJson send009ToHt(Long id) {
        TbOrder order = tbOrderService.getById(id);
        if (order == null) {
            throw new AjaxError("订单不存在");
        }
        order.setPeopleConfirmStatus(1)
				.setPeopleConfirmTime(new Date())
				.setCooperEntrustStatus(1)
                .setApplyConfirmStatus(1);
        tbOrderService.updateById(order);
        tbOrderService.sendConfirmMsgToHt(order);
        return AjaxJson.getSuccess();
    }

    /**
     * 订单补推结关信息给银行
     */
    @RequestMapping("sendCXB001")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_SENDCXB001)
    public AjaxJson sendCXB001(Long id) {
        tbOrderService.sendCXB001(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 订单补推结关信息给银行——按车，并且未完成
     */
    @RequestMapping("sendCXB001ByVeNo")
    @SaCheckPermission(TbOrder.PERMISSION_CODE_SENDCXB001)
    public AjaxJson sendCXB001ByVeNo(String veNo, Long id) {
        tbOrderService.sendCXB001ByVeNo(veNo, id);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("getSendCXB001ByVeNoList")
    @SaCheckPermission(TbOrder.PERMISSION_CODE)
    public AjaxJson getSendCXB001ByVeNoList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrder> list = tbOrderService.getSendCXB001ByVeNoList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /*-----           正常业务逻辑👆👆    rpc远程调用👇👇 -----*/


    /**
     * 系统接收到航通接口信息：进出口申报单退单
     **/
    @RequestMapping("rpc/chargeback")
    public boolean chargeback(@RequestBody HtDeclareChargebackDto htDeclareChargebackDto) {
        return tbOrderService.chargeback(htDeclareChargebackDto);
    }

    @RequestMapping("rpc/getOrderListByMainIds")
    public List<OrderDto> getOrderListByMainIds(String mainIds) {
        return tbOrderService.getOrderListByMainIds(mainIds);
    }

    /**
     * 系统接收到航通接口信息：车辆已出一级市场 ,相对应的一级市场订单状态修改
     **/
    @RequestMapping("rpc/orderFinish")
    public boolean orderFinish(@RequestBody HtPassCardDTO htPassCardDTO) {
        return tbOrderService.orderFinish(htPassCardDTO);
    }

    public static void main(String[] args) {
        System.out.println(RandomUtil.randomString(32));
    }


}
