package com.fjhx.sale.service.pack.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.sale.entity.pack.po.Pack;
import com.fjhx.sale.entity.pack.po.PackDetail;
import com.fjhx.sale.entity.pack.po.PackDetailGoods;
import com.fjhx.sale.entity.pack.po.PackDetailProduct;
import com.fjhx.sale.mapper.pack.PackMapper;
import com.fjhx.sale.service.pack.PackDetailGoodsService;
import com.fjhx.sale.service.pack.PackDetailProductService;
import com.fjhx.sale.service.pack.PackDetailService;
import com.fjhx.sale.service.pack.PackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.sale.util.code.CodeEnum;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.sale.entity.pack.vo.PackVo;
import com.fjhx.sale.entity.pack.dto.PackSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.sale.entity.pack.dto.PackDto;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 装箱出货表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-18
 */
@Service
public class PackServiceImpl extends ServiceImpl<PackMapper, Pack> implements PackService {

    @Autowired
    private PackDetailService packDetailService;

    @Autowired
    private PackDetailProductService packDetailProductService;

    @Autowired
    private PackDetailGoodsService packDetailGoodsService;

    @Autowired
    private CodingRuleService codingRuleService;

    @Override
    public Page<PackVo> getPage(PackSelectDto dto) {
        IWrapper<Pack> wrapper = getWrapper();
        wrapper.orderByDesc("p", Pack::getId);
        Page<PackVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public PackVo detail(Long id) {
        Pack Pack = this.getById(id);
        PackVo result = BeanUtil.toBean(Pack, PackVo.class);
        return result;
    }

    /**
     * 新增
     * @param pack
     */
    @Override
    @DSTransactional
    public void add(Pack pack) {
        if(StringUtils.isEmpty(pack.getContractIds())){
            throw new ServiceException("参数异常");
        }
//        pack.setCode(CodeEnum.PACK.getCode());
        pack.setCode(codingRuleService.createCode(CodingRuleEnum.PACK.getKey(),null));
        this.save(pack);
        List<PackDetail> packDetailList = pack.getPackDetailList();
        if(CollectionUtils.isNotEmpty(packDetailList)){
            List<PackDetailProduct> packDetailProductList = new ArrayList<>();
            List<PackDetailGoods> packDetailGoodsList = new ArrayList<>();
            for(PackDetail p:packDetailList){
                p.setPackId(pack.getId());
                p.setId(IdWorker.getId());
                //处理装箱产品明细
                setPackDetailProducts(p,packDetailProductList);
                //处理自定义装箱货物
                setPackDetailGoods(p,packDetailGoodsList);
            }
            packDetailService.saveBatch(packDetailList);
            packDetailGoodsService.saveBatch(packDetailGoodsList);
            packDetailProductService.saveBatch(packDetailProductList);
        }
    }

    /**
     * 处理装箱产品明细
     * @param packDetail
     * @param packDetailProductList
     */
    private void setPackDetailProducts(PackDetail packDetail,List<PackDetailProduct> packDetailProductList){
        List<PackDetailProduct> list = packDetail.getPackDetailProductList();
        if(CollectionUtils.isNotEmpty(list)){
            for(PackDetailProduct p:list){
                p.setPackDetailId(packDetail.getId());
                packDetailProductList.add(p);
            }
        }
    }
    /**
     * 处理装箱产品明细
     * @param packDetail
     * @param packDetailGoodsList
     */
    private void setPackDetailGoods(PackDetail packDetail,List<PackDetailGoods> packDetailGoodsList){
        List<PackDetailGoods> list = packDetail.getPackDetailGoodsList();
        if(CollectionUtils.isNotEmpty(list)){
            for(PackDetailGoods p:list){
                p.setPackDetailId(packDetail.getId());
                packDetailGoodsList.add(p);
            }
        }
    }

    @Override
    public void edit(PackDto packDto) {
        this.updateById(packDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
