package com.fjhx.service.quality.impl;

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.base.Condition;
import com.fjhx.entity.quality.QualityTestingDetails;
import com.fjhx.mapper.quality.QualityTestingDetailsMapper;
import com.fjhx.params.quality.QualityTestingDetailsVo;
import com.fjhx.service.quality.QualityTestingDetailsService;
import com.fjhx.service.quality.QualityTestingService;
import com.fjhx.utils.Assert;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 质检明细 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-26
 */
@Service
public class QualityTestingDetailsServiceImpl extends ServiceImpl<QualityTestingDetailsMapper, QualityTestingDetails> implements QualityTestingDetailsService {

    @Autowired
    private QualityTestingService qualityTestingService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(QualityTestingDetailsVo qualityTestingDetailsVo) {
        save(qualityTestingDetailsVo);
        // 更新主表质检数量
        qualityTestingService.edit(1, qualityTestingDetailsVo.getQualityTestingId());
    }

    @Override
    public void edit(QualityTestingDetailsVo qualityTestingDetailsVo) {
        updateById(qualityTestingDetailsVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(QualityTestingDetailsVo qualityTestingDetailsVo) {
        removeById(qualityTestingDetailsVo.getId());
        // 更新主表质检数量
        qualityTestingService.edit(-1, qualityTestingDetailsVo.getQualityTestingId());
    }

    @Override
    public Map<String, Object> recordDetailStatistics(Condition condition) {

        Long qualityTestingId = condition.getLong("qualityTestingId");
        Assert.notEmpty(qualityTestingId, "质检id不能为空");

        return getMap(Wrappers.<QualityTestingDetails>query()
                .select(
                        "count(0) count",
                        "ifnull(sum(if(check_judgment=0 or check_judgment=1,1,0)),0) qualifiedCount",
                        "ifnull(sum(if(check_judgment=2,1,0)),0) unqualifiedCount")
                .lambda()
                .eq(QualityTestingDetails::getQualityTestingId, qualityTestingId)
        );

    }

    @Override
    public Page<QualityTestingDetails> recordDetailPage(Condition condition) {

        Long qualityTestingId = condition.getLong("qualityTestingId");
        Assert.notEmpty(qualityTestingId, "质检id不能为空");

        Integer status = condition.getStatus();

        return lambdaQuery()
                .eq(QualityTestingDetails::getQualityTestingId, qualityTestingId)
                .func(ObjectUtil.isNotEmpty(status), q -> {
                    if (status.equals(1)) {
                        q.in(QualityTestingDetails::getCheckJudgment, 0, 1);
                    } else {
                        q.eq(QualityTestingDetails::getCheckJudgment, 2);
                    }
                })
                .page(condition.getPage());
    }

}
