package com.fjhx.purchase.service.quality.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.purchase.entity.arrival.enums.ArrivalDetailStatusEnum;
import com.fjhx.purchase.entity.arrival.po.ArrivalDetail;
import com.fjhx.purchase.entity.quality.dto.QualityDto;
import com.fjhx.purchase.entity.quality.dto.QualitySelectDto;
import com.fjhx.purchase.entity.quality.po.Quality;
import com.fjhx.purchase.entity.quality.po.QualityDetail;
import com.fjhx.purchase.entity.quality.vo.QualityVo;
import com.fjhx.purchase.mapper.quality.QualityMapper;
import com.fjhx.purchase.service.arrival.ArrivalDetailService;
import com.fjhx.purchase.service.quality.QualityDetailService;
import com.fjhx.purchase.service.quality.QualityService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 质检 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-11
 */
@DS(SourceConstant.PURCHASE)
@Service
public class QualityServiceImpl extends ServiceImpl<QualityMapper, Quality> implements QualityService {

    @Autowired
    private QualityDetailService qualityDetailService;

    @Autowired
    private ArrivalDetailService arrivalDetailService;


    @Autowired
    private CodingRuleService codingRuleService;

    @Override
    public Page<QualityVo> getPage(QualitySelectDto dto) {
        IWrapper<Quality> wrapper = getWrapper();
        wrapper.orderByDesc("q", Quality::getId);
        Page<QualityVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

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

    /**
     * 添加
     * @param quality
     */
    @Override
    @DSTransactional
    public void add(Quality quality) {
//        quality.setCode(CodeEnum.QUALITY.getCode());
        quality.setCode(codingRuleService.createCode(CodingRuleEnum.QUALITY.getKey(),null));
        this.save(quality);
        List<QualityDetail> qualityDetailList = quality.getQualityDetailList();
        if(CollectionUtils.isNotEmpty(qualityDetailList)){
            List<ArrivalDetail> upAll = new ArrayList<>();
            for(QualityDetail q:qualityDetailList){
                if(q.getQualifiedCount().compareTo(BigDecimal.ZERO)==0
                        &&q.getNoQualifiedCount().compareTo(BigDecimal.ZERO)==0){
                    throw new ServiceException("当前质检数量不能为0");
                }
                q.setQualityId(quality.getId());
                //赋值质检状态
                BigDecimal qualityCount = qualityDetailService.getSumQualityCount(q.getArrivalDetailId());
                //查询到货数量
                ArrivalDetail arrivalDetail = arrivalDetailService.getById(q.getArrivalDetailId());
                BigDecimal sum = qualityCount.add(q.getQualifiedCount().add(q.getNoQualifiedCount()));
                ArrivalDetail up = new ArrivalDetail();
                up.setId(q.getArrivalDetailId());
                if(sum.compareTo(arrivalDetail.getCount())>-1){//如果质检数量大于等于到货数量
                    up.setStatus(ArrivalDetailStatusEnum.STATUS_20.getKey());
                }else{
                    up.setStatus(ArrivalDetailStatusEnum.STATUS_10.getKey());
                }
                upAll.add(up);
            }
            arrivalDetailService.updateBatchById(upAll);
            qualityDetailService.saveBatch(qualityDetailList);
        }
    }

    @Override
    public void edit(QualityDto qualityDto) {
        this.updateById(qualityDto);
    }

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

}
