package com.unis.reserveLayout.modular.qualityInspectionOrg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.common.constant.DBConstant;
import com.unis.reserveLayout.modular.qualityInspectionOrg.entity.BusinessQcQualityInspection;
import com.unis.reserveLayout.modular.qualityInspectionOrg.entity.BusinessQcQualityInspectionItem;
import com.unis.reserveLayout.modular.qualityInspectionOrg.mapper.QualityInspectionItemMapper;
import com.unis.reserveLayout.modular.qualityInspectionOrg.mapper.QualityInspectionMapper;
import com.unis.reserveLayout.modular.qualityInspectionOrg.param.QualityInspectionPageParam;
import com.unis.reserveLayout.modular.qualityInspectionOrg.param.QualityInspectionReportPageParam;
import com.unis.reserveLayout.modular.qualityInspectionOrg.service.QualityInspectionService;
import com.unis.reserveLayout.modular.utils.DateUtil;
import com.unis.reserveLayout.modular.utils.GenerateNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description:
 * @create 2024-06-11 17:21
 */
@Service
@DS(value = DBConstant.DS_DEPOT)
public class QualityInspectionServiceImpl extends ServiceImpl<QualityInspectionMapper, BusinessQcQualityInspection> implements QualityInspectionService {
    @Resource
    private QualityInspectionMapper qualityInspectionMapper;
    @Resource
    private QualityInspectionItemMapper qualityInspectionItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BusinessQcQualityInspection param) {
        // 生成质检报告单(检验类别+检验日期(yyyyMMdd)+4位顺序号组成)
        param.setZjbgdh(this.GetZjbgdh(param));
        qualityInspectionMapper.insert(param);
        // 保存子表
        List<BusinessQcQualityInspectionItem> items = param.getItems();
        if (CollectionUtils.isNotEmpty(items)) {
            items.forEach(item -> {
                item.setQcQualityInspectionId(param.getId());
                qualityInspectionItemMapper.insert(item);
            });
        }
    }

    // // 生成质检报告单(检验类别+检验日期(yyyyMMdd)+4位顺序号组成)
    private synchronized String GetZjbgdh(BusinessQcQualityInspection saveParam) {
        // 查询最后一个顺序号
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", saveParam.getOrgId());
        map.put("basicStorehouseId", saveParam.getBasicStorehouseId());
        map.put("warehouseId", saveParam.getWarehouseId());
        String xh = qualityInspectionMapper.selectOnly(map);
        // 初始化
        int num = 1;
        if (!StringUtils.isEmpty(xh)) {
            String substring = xh.substring(xh.length() - 4);
            int i = Integer.parseInt(substring);
            if (i < 9999)
                num += i;
        }
        return String.format(
                "%s%s%s", saveParam.getJylb(),
                DateUtil.getYMD(saveParam.getJysj()),
                GenerateNumberUtil.integerToString(4, num)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BusinessQcQualityInspection param) {
        qualityInspectionMapper.updateById(param);
        // 修改子表
        List<BusinessQcQualityInspectionItem> items = param.getItems();
        // 先删除
        QueryWrapper<BusinessQcQualityInspectionItem> wrapper = new QueryWrapper<>();
        wrapper.eq("qc_quality_inspection_id", param.getId());
        qualityInspectionItemMapper.delete(wrapper);
        // 在新增
        if (CollectionUtils.isNotEmpty(items)) {
            items.forEach(item -> {
                item.setQcQualityInspectionId(param.getId());
                qualityInspectionItemMapper.insert(item);
            });
        }
    }

    @Override
    public BusinessQcQualityInspection detail(Integer id) {
        // 查询主表
        BusinessQcQualityInspection businessQcQualityInspection = qualityInspectionMapper.selectById(id);
        if (ObjectUtils.isNotEmpty(businessQcQualityInspection)) {
            QueryWrapper<BusinessQcQualityInspectionItem> wrapper = new QueryWrapper<>();
            wrapper.eq("qc_quality_inspection_id", id);
            List<BusinessQcQualityInspectionItem> items = qualityInspectionItemMapper.selectList(wrapper);
            if (CollectionUtils.isNotEmpty(items)) {
                businessQcQualityInspection.setItems(items);
            }
        }
        return businessQcQualityInspection;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        // 删除主表数据
        qualityInspectionMapper.deleteById(id);
        // 删除子表数据
        QueryWrapper<BusinessQcQualityInspectionItem> wrapper = new QueryWrapper<>();
        wrapper.eq("qc_quality_inspection_id", id);
        qualityInspectionItemMapper.delete(wrapper);
    }

    @Override
    public Page<BusinessQcQualityInspection> pageList(QualityInspectionPageParam pageParam) {
        return qualityInspectionMapper.pageList(new Page<>(pageParam.getCurrent(), pageParam.getSize()), pageParam.getStorehouseName());
    }

    @Override
    public Page<BusinessQcQualityInspection> reportPageList(QualityInspectionReportPageParam pageParam) {
        return qualityInspectionMapper.reportPageList(new Page<>(pageParam.getCurrent(), pageParam.getSize()), pageParam);
    }
}
