package com.pj.project.tb_business;

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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.pj.constants.UserTypeEnum;
import com.pj.project.tb_business_item.TbBusinessItem;
import com.pj.project.tb_business_item.TbBusinessItemService;
import com.pj.project.tb_pass_record.TbPassRecord;
import com.pj.project.tb_pass_record.TbPassRecordService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;

import javax.annotation.Resource;


/**
 * Controller: tb_business -- 入境登记
 *
 * @author qzy
 */
@RestController
@RequestMapping("/TbBusiness/")
public class TbBusinessController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbBusinessService tbBusinessService;
    @Resource
    private TbBusinessItemService tbBusinessItemService;
    @Resource
    private TbPassRecordService tbPassRecordService;

    /**
     * 增
     */
    @RequestMapping("add")
    @SaCheckPermission(TbBusiness.PERMISSION_CODE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson add(TbBusiness t) {
        return tbBusinessService.addOrUpdate(t);
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    @SaCheckPermission(TbBusiness.PERMISSION_INPUT)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson delete(String id) {
        tbBusinessService.removeById(id);
        tbBusinessItemService.removeByBusinessId(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    @SaCheckPermission(TbBusiness.PERMISSION_CODE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        ids.forEach(id -> {
            tbBusinessItemService.removeByBusinessId(id + "");
        });
        int line = SP.publicMapper.deleteByIds(TbBusiness.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改
     */
    @RequestMapping("update")
    @SaCheckPermission(TbBusiness.PERMISSION_CODE)
    public AjaxJson update(TbBusiness t) {
        tbBusinessService.addOrUpdate(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 确认业务
     */
    @RequestMapping("confirm")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson confirm() {
        if (!StpUtil.hasPermissionAnd(TbBusiness.PERMISSION_CONFIRM) && !
                StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无确认业务权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        List<String> ids = so.getListByComma("ids", String.class);
        tbBusinessService.confirm(ids);
        return AjaxJson.getSuccess();
    }

    /**
     * 确认业务
     */
    @RequestMapping("adminConfirmPay")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson adminConfirmPay() {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无确认支付权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        List<String> ids = so.getListByComma("ids", String.class);
        String ticket = so.getString("payTicket");
        tbBusinessService.adminConfirmPay(ids, ticket);
        return AjaxJson.getSuccess();
    }

    /**
     * 手动入场确认
     */
    @RequestMapping("adminConfirmIn")
    @Transactional(rollbackFor = Exception.class)
    public AjaxJson adminConfirmIn() {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无确认入场权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        String id = so.getString("id");
        String inChannel = so.getString("inChannel");
        tbBusinessService.adminConfirmIn(id, inChannel);
        return AjaxJson.getSuccess();
    }


    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(String id) {
        TbBusiness t = tbBusinessService.getById(id);
        List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(id);
        t.setItems(items);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        String currentCustomerId = StpUserUtil.getCustomerId();
        if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("customerId", currentCustomerId);
        }
        List<TbBusiness> list = tbBusinessService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 改
     */
    @RequestMapping("pay")
    public AjaxJson pay() {
        if (!StpUtil.hasPermissionAnd("tb-business-pay") && !
                StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无支付权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        String id = so.getString("id");
        String payTicket = so.getString("payTicket");
        tbBusinessService.pay(id, payTicket);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("adminSetIn")
    public AjaxJson adminSetIn() {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无入场权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        String id = so.getString("id");
        Date inTime = so.getDateTime("realInTime");
        Date chinaCarInTime = so.getDateTime("chinaCarInTime");
        TbBusiness tbBusiness = tbBusinessService.getById(id);
        tbBusiness.setChinaCarInTime(chinaCarInTime).setRealInTime(inTime);
        tbBusinessService.updateById(tbBusiness);

        //更新放行记录
        TbPassRecord record = tbPassRecordService.getByBusinessId(tbBusiness.getId());
        record.setStatus(2).setInTime(tbBusiness.getRealInTime()).setUpdateTime(new Date());
        tbPassRecordService.saveOrUpdate(record);

        return AjaxJson.getSuccess();
    }

    @RequestMapping("adminOut")
    public AjaxJson adminOut(){
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无离场权限");
        }
        SoMap so = SoMap.getRequestSoMap();
        String id = so.getString("id");
       TbBusiness business= tbBusinessService.getById(id);
       Date outTime=so.getDateTime("outDayTime");
       Date chinaCarOutTime=so.getDateTime("chinaCarOutTime");
        business.setOutDayTime(outTime);
        tbBusinessService. calculationPartMoney("越南车",business);
        tbBusinessService.updateById(business);
        business.setChinaCarOutTime(chinaCarOutTime);
        tbBusinessService. calculationPartMoney("中国车",business);
        tbBusinessService.updateById(business);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("calJwPartMoney")
    public AjaxJson calJwPartMoney(TbBusiness business) {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无生成账单权限");
        }
        tbBusinessService.calculationPartMoney("越南车", business);
        return AjaxJson.getSuccessData(business);
    }

    @RequestMapping("calChinaPartMoney")
    public AjaxJson calChinaPartMoney(TbBusiness business) {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无生成账单权限");
        }
        tbBusinessService.calculationPartMoney("中国车", business);
        return AjaxJson.getSuccessData(business);
    }
    @RequestMapping("complete")
    public AjaxJson complete(TbBusiness business) {
        if (!StrUtil.equals(StpUserUtil.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getError("无生成账单权限");
        }
        business.setComplete(1);
        tbBusinessService.updateById(business);
        return AjaxJson.getSuccessData(business);
    }


    // ------------------------- 前端接口 -------------------------


}
