package com.pj.ht_trade_settlement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.api.dto.BorderBuyGoodsListDto;
import com.pj.api.dto.BordersListDto;
import com.pj.api.dto.TradeSettlementDto;
import com.pj.current.config.SystemObject;
import com.pj.enummj.*;
import com.pj.tb_goods.TbGoods;
import com.pj.tb_goods.TbGoodsService;
import com.pj.tb_order.TbOrder;
import com.pj.tb_order.TbOrderMapper;
import com.pj.tb_people.TbPeople;
import com.pj.tb_people.TbPeopleMapper;
import com.pj.tb_people.TbPeopleService;
import com.pj.tb_shop.TbShop;
import com.pj.tb_shop.TbShopService;
import com.pj.tb_trade_area.TbTradeArea;
import com.pj.tb_trade_area.TbTradeAreaService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Service: ht_trade_settlement -- 航通交易结算表
 *
 * @author yzs
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HtTradeSettlementService extends ServiceImpl<HtTradeSettlementMapper, HtTradeSettlement> implements IService<HtTradeSettlement> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    HtTradeSettlementMapper htTradeSettlementMapper;
    @Autowired
    private TbPeopleService tbPeopleService;
    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Resource
    private TbTradeAreaService tbTradeAreaService;
    @Resource
    private TbGoodsService tbGoodsService;
    @Resource
    private TbShopService tbShopService;

    /**
     * 增
     */
    void add(HtTradeSettlement h) {
        save(h);
    }

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

    /**
     * 改
     */
    void update(HtTradeSettlement h) {
        updateById(h);

    }

    /**
     * 查
     */
    HtTradeSettlement getById(String platSeqNo) {
        return super.getById(platSeqNo);
    }

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


    /**
     * 远程调用: 添加大订单时同事生成小定单
     */
    public boolean tradeSettlementDto(TradeSettlementDto tradeSettlementDto) {
        HtTradeSettlement htTradeSettlement = new HtTradeSettlement();
        BeanUtils.copyProperties(tradeSettlementDto, htTradeSettlement);
        int insert = htTradeSettlementMapper.insert(htTradeSettlement);
        //取出边民小定单集合
        List<BordersListDto> bordersListDtoList = tradeSettlementDto.getBordersListDtoList();
        BorderBuyGoodsListDto borderBuyGoodsListDto = tradeSettlementDto.getBorderBuyGoodsListDtoList().get(0);
        TbGoods tbGoods = tbGoodsService.findByCode(borderBuyGoodsListDto.getCodeTs());
        TbTradeArea tbTradeArea = tbTradeAreaService.updateHandler(tradeSettlementDto.getFieldCode(), tradeSettlementDto.getFieldName());
        TbShop tbShop = tbShopService.findByCode(tradeSettlementDto.getShopNo());
        if (tbShop != null) {
            tbShop.setEnterpriseId(tbShop.getEnterpriseId())
                    .setEnterpriseName(tbShop.getEnterpriseName());
        }
        //循环
        if (bordersListDtoList.size() > 0) {
            bordersListDtoList.forEach(item -> {
                //开始创建小定单
                TbOrder tbOrder = new TbOrder();
                //航通字段
                BeanUtils.copyProperties(item, tbOrder);
                //根据身份证查询边民
                TbPeople tbPeople = tbPeopleService.findByIdCarNo(item.getIdno());
                tbOrder.setCallCarStatus(CallCarStatus.CALL_CAR_STATUS_ZERO.getCode());
                tbOrder.setDeleteStatus(DeleteStatus.DELETE_STATUS_ON.getCode());
                tbOrder.setCreateTime(new Date())
                        .setTradeNo(SystemObject.getREFcode("YJSCDD", new Date()))
                        .setTradeAreaId(tbTradeArea.getId()).setTradeAreaName(tbTradeArea.getName());
                tbOrder.setResaleStatus(ResaleStatus.RESALE_STATUS_ZERO.getCode());
                tbOrder.setFinishStatus(OrderStatus.ORDER_STATUS_ZERO.getCode());
                tbOrder.setSend(DeliverStatus.DELIVER_STATUS_ZERO.getCode());
                tbOrder.setApply(ApplyStatus.APPLY_STATUS_ZERO.getCode());
                tbOrder.setGoodsId(tbGoods.getId()).setTotalPrice(borderBuyGoodsListDto.getBuyAmount())
                        .setTotalWeight(borderBuyGoodsListDto.getGrossWt())
                        .setGoodsNames(tbGoods.getGName())
                        .setGoodsUnit(tbGoods.getGUnit());
                if (tbPeople != null) {
                    tbOrder.setBuyUserId(tbPeople.getId());
                    tbOrder.setBuyUserName(tbPeople.getBorderName());
                    tbOrder.setGroupId(tbPeople.getGroupId());
                }
                tbOrder.setPreNo(item.getPreNo());

                tbOrderMapper.insert(tbOrder);
            });
        }
        return insert == 1;
    }
}
