package com.pj.tb_import_order;

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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pj.api.dto.HtIeportDeclBillsBpLimitDto;
import com.pj.api.dto.HtIeportDeclBillsGoodsDto;
import com.pj.api.dto.HtImportOrderDto;
import com.pj.common.core.exception.ServiceException;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project4sp.SP;
import com.pj.tb_import_goods.TbImportGoods;
import com.pj.tb_import_goods.TbImportGoodsMapper;
import com.pj.tb_order.TbOrder;
import com.pj.tb_order.TbOrderService;
import com.pj.tb_people.TbPeopleMapper;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service: tb_import_order -- 进口申报单
 *
 * @author lbl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbImportOrderService extends ServiceImpl<TbImportOrderMapper, TbImportOrder> implements IService<TbImportOrder> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbImportOrderMapper tbImportOrderMapper;
    @Autowired
    TbOrderService tbOrderService;
    @Autowired
    TbImportGoodsMapper importGoodsMapper;


    /**
     * 增
     */
    void add(TbImportOrder t) {
        save(t);
    }

    /**
     * 删
     */
    void delete(Long id) {
        removeById(id);
    }

    /**
     * 改
     */
    void update(TbImportOrder t) {
        updateById(t);

    }

    /**
     * 查
     */
    TbImportOrder getById(Long id) {
        return super.getById(id);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbImportOrder> getList(SoMap so) {
        return tbImportOrderMapper.getList(so);
    }

    /**
     * 查看订单详情
     *
     * @param orderId 一级市场订单ID
     * @return 订单的vo
     */
    public ImportOrderVo importOrderDetail(Long orderId) {
        TbOrder peopleOrder = tbOrderService.getById(orderId);
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        if (!appLoginInfo.getFk().equals(peopleOrder.getBuyUserId()))
            throw new ServiceException("当前边民订单信息异常,请联系管理员!");

        //根据边民订单获取关联的进口申报单Id，查询进口申报单详情
        TbImportOrder importOrder = tbImportOrderMapper.selectById(peopleOrder.getImportOrderId());
        if (importOrder == null) throw new ServiceException("进口申报单信息异常,请联系管理员!");

        ImportOrderVo importOrderVo = new ImportOrderVo();
        BeanUtils.copyProperties(importOrder, importOrderVo);
        return importOrderVo;
    }

    public boolean audit(Long id, Integer judgeStatus, String judgeContent) {
        TbImportOrder importOrder = tbImportOrderMapper.selectById(id);
        if (importOrder == null) throw new ServiceException("进口申报单信息异常,请联系管理员!");
        String name = StpUserUtil.getLoginName();
        importOrder.setJudgeStatus(judgeStatus).setJudgeContent(judgeContent).setJudgeTime(new Date()).setJudgeBy(name);
        return updateById(importOrder);
    }

    /**
     * 远程调用 进口申报单同步
     */
    public boolean importOrderDto(HtImportOrderDto importOrderDto) {
        //委托边民额度
        List<HtIeportDeclBillsBpLimitDto> ieportDeclBillsBpLimitDtoList = JSON.parseArray(importOrderDto.getBorderPeopleLimitList(), HtIeportDeclBillsBpLimitDto.class);
        ieportDeclBillsBpLimitDtoList.forEach(ieportDeclBillsBpLimitDto -> {
            String peopleIdCardNo = ieportDeclBillsBpLimitDto.getCiphertextIdno();
            TbImportOrder importOrder = tbImportOrderMapper.findByManifestId(importOrderDto.getManifestId(), peopleIdCardNo);
            if (importOrder == null) {
                importOrder = new TbImportOrder();
                importOrder.setCreateTime(new Date());
            }
            BeanUtils.copyProperties(importOrderDto, importOrder, "id");
            importOrder.setGoodsNames(ieportDeclBillsBpLimitDto.getgName())
                    .setGLimit(ieportDeclBillsBpLimitDto.getgLimit())
                    .setPeopleIdNo(peopleIdCardNo)
                    .setPeopleName(ieportDeclBillsBpLimitDto.getBorderName())
                    .setGGrossWt(ieportDeclBillsBpLimitDto.getgGrossWt());
            saveOrUpdate(importOrder);
            long importOrderId = importOrder.getId();
            LambdaUpdateWrapper<TbOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(TbOrder::getLimitNo, ieportDeclBillsBpLimitDto.getLimitNo());
            wrapper.set(TbOrder::getGLimit, ieportDeclBillsBpLimitDto.getgLimit());
            wrapper.set(TbOrder::getGNo, ieportDeclBillsBpLimitDto.getgNo());
            wrapper.set(TbOrder::getImportOrderId, importOrderId);
            wrapper.eq(TbOrder::getPreIeportNo, ieportDeclBillsBpLimitDto.getPlatSeqNo());
            wrapper.eq(TbOrder::getIdno, peopleIdCardNo);
            tbOrderService.update(wrapper);
        });
        return true;
    }
}
