package com.fjhx.service.quality.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.purchase.Purchase;
import com.fjhx.entity.quality.QualityDetails;
import com.fjhx.entity.quality.QualityInfo;
import com.fjhx.entity.supplier.Supplier;
import com.fjhx.mapper.quality.QualityInfoMapper;
import com.fjhx.params.quality.*;
import com.fjhx.service.product.ProductInfoService;
import com.fjhx.service.quality.QualityDetailsService;
import com.fjhx.service.quality.QualityInfoService;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import com.fjhx.utils.wrapperUtil.KeywordData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 质检 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-02-13
 */
@Service
public class QualityInfoServiceImpl extends ServiceImpl<QualityInfoMapper, QualityInfo> implements QualityInfoService {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private QualityDetailsService qualityDetailsService;

    @Override
    public Page<QualityInfoPageVo> getPage(QualityInfoPageDto dto) {

        IWrapper<Object> wrapper = IWrapper.getWrapper(dto)
                .keyword(new KeywordData("p", Purchase::getCode), new KeywordData("s", Supplier::getName))
                .eq("qi", QualityInfo::getStatus, dto.getStatus());

        Page<QualityInfoPageVo> page = baseMapper.getPage(dto.getPage(), wrapper);
        List<QualityInfoPageVo> records = page.getRecords();

        if (records.size() == 0) {
            return page;
        }

        // 赋值采购人名称
        Map<Long, String> userNameMap = UserClientUtil.getUserNameMap(records, QualityInfoPageVo::getCreateUser);
        for (QualityInfoPageVo record : records) {
            record.setCreateUserName(userNameMap.get(record.getCreateUser()));
        }

        return page;
    }

    @Override
    public void add(QualityInfoVo qualityInfoVo) {
        save(qualityInfoVo);
    }

    @Override
    public void edit(QualityInfoVo qualityInfoVo) {
        updateById(qualityInfoVo);
    }

    @Override
    public void delete(QualityInfoVo qualityInfoVo) {
        removeById(qualityInfoVo.getId());
    }

    @Override
    public List<QualityDetailsVo> getDetailsList(GetDetailsListDto dto) {
        List<QualityDetails> list = qualityDetailsService.list(q -> q.eq(QualityDetails::getQualityInfoId, dto.getQualityInfoId()));

        for (QualityDetails qualityDetails : list) {
            qualityDetails.setTotalQuantity(qualityDetails.getTotalQuantity()
                    .subtract(qualityDetails.getDisqualificationQuantity())
                    .subtract(qualityDetails.getQualifiedQuantity()));

            qualityDetails.setDisqualificationQuantity(BigDecimal.ZERO);
            qualityDetails.setQualifiedQuantity(BigDecimal.ZERO);
        }

        List<QualityDetailsVo> result = BeanUtil.copyToList(list, QualityDetailsVo.class);

        productInfoService.attributeAssign(result, QualityDetails::getProductInfoId, (item, product) -> {
            item.setProductCode(product.getCode());
            item.setProductName(product.getName());
        });

        return result;
    }

}
