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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.utils.Assert;
import com.fjhx.sale.entity.pack.dto.PackDetailDto;
import com.fjhx.sale.entity.pack.dto.PackDetailSelectDto;
import com.fjhx.sale.entity.pack.enums.PackShipmentStatusEnum;
import com.fjhx.sale.entity.pack.po.PackDetail;
import com.fjhx.sale.entity.pack.po.PackShipment;
import com.fjhx.sale.entity.pack.vo.PackDetailVo;
import com.fjhx.sale.mapper.pack.PackDetailMapper;
import com.fjhx.sale.service.pack.PackDetailService;
import com.fjhx.sale.service.pack.PackShipmentService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 装箱出货明细表	 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-18
 */
@Service
public class PackDetailServiceImpl extends ServiceImpl<PackDetailMapper, PackDetail> implements PackDetailService {

    @Autowired
    private PackShipmentService packShipmentService;

    /**
     * 分页
     *
     * @param dto
     * @return
     */
    @Override
    public Page<PackDetailVo> getPage(PackDetailSelectDto dto) {
        IWrapper<PackDetail> wrapper = getWrapper();
        if (ObjectUtil.isNotEmpty(dto.getShipmentStatus())) {
            wrapper.eq("pd", PackDetailVo::getShipmentStatus, dto.getShipmentStatus());
        }
        if (StringUtils.isNotEmpty(dto.getKeyword())) {
//            wrapper.keyword(dto.getKeyword(), new SqlField("t3.`code`"), new SqlField("t2.`product_name`"));
            wrapper.like("pd.codeAPName", dto.getKeyword());
        }
//        wrapper.groupBy("t1.pack_id");
//        wrapper.orderByDesc("t1.create_time");
        Page<PackDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

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

    @Override
    public void add(PackDetailDto packDetailDto) {
        this.save(packDetailDto);
    }

    @Override
    public void edit(PackDetailDto packDetailDto) {
        this.updateById(packDetailDto);
    }

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

    /**
     * 出货
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shipment(List<Long> ids, Long contractId) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new ServiceException("最少选中一条记录");
        }
        if (ObjectUtil.isEmpty(contractId)) {
            throw new ServiceException("必须选择一条主合同");
        }
        //查询id对应的装箱id 把装箱id下的记录都出货
        List<PackDetail> packDetails = this.listByIds(ids);
        Assert.notEmpty(packDetails, "查询不到装箱明细信息");
        if (packDetails.size() != ids.size()) {
            throw new ServiceException("存在未知装箱明细");
        }
        List<Long> packIds = packDetails.stream().map(PackDetail::getPackId).collect(Collectors.toList());
        this.update(Wrappers.<PackDetail>update().lambda()
                .set(PackDetail::getShipmentStatus, PackShipmentStatusEnum.STATUS_1.getKey())
                .set(PackDetail::getShipmentTime, new Date())
                .in(PackDetail::getPackId, packIds));
        //添加一条绑定主合同记录
        PackShipment packShipment = new PackShipment();
        packShipment.setPackDetailIds(StringUtils.join(ids, ","));
        packShipment.setContractId(contractId);
        packShipmentService.save(packShipment);
    }

}
