/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.unis.basic.info.modular.enumInfo.mapper.BasicEnumMapper;
import com.unis.basic.info.modular.orgInfo.entity.OrgInfo;
import com.unis.basic.info.modular.orgInfo.mapper.OrgInfoMapper;
import com.unis.basic.info.modular.storehouseInfo.entity.StorehouseInfo;
import com.unis.basic.info.modular.storehouseInfo.mapper.StorehouseInfoMapper;
import com.unis.basic.info.modular.unitInfo.entity.BasicEnum;
import com.unis.basic.info.modular.unitInfo.mapper.EnumMapper;
import com.unis.basic.info.modular.warehouseInfo.entity.WarehouseInfo;
import com.unis.basic.info.modular.warehouseInfo.mapper.WarehouseInfoMapper;
import com.unis.safeStorage.modular.dataKcglKcswDefault.entity.DataKcglKcswDefault;
import com.unis.safeStorage.modular.dataKcglKcswDefault.mapper.DataKcglKcswDefaultMapper;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspection.entity.SsSafeGrainStorageInspection;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspection.mapper.SsSafeGrainStorageInspectionMapper;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.param.*;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.vo.SsSafeGrainStorageInspectionResultVO;
import com.unis.safeStorage.modular.ssSafeProductInspection.entity.SsSafeProductInspection;
import com.unis.safeStorage.modular.ssSafeProductInspection.mapper.SsSafeProductInspectionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.exception.CommonException;
import com.unis.common.page.CommonPageRequest;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.entity.SsSafeGrainStorageInspectionResult;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.mapper.SsSafeGrainStorageInspectionResultMapper;
import com.unis.safeStorage.modular.ssSafeGrainStorageInspectionResult.service.SsSafeGrainStorageInspectionResultService;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 安全仓储-安全储粮检查表-结果Service接口实现类
 *
 * @author hjc
 * @date 2024/06/03 10:48
 **/
@Service
public class SsSafeGrainStorageInspectionResultServiceImpl extends ServiceImpl<SsSafeGrainStorageInspectionResultMapper, SsSafeGrainStorageInspectionResult> implements SsSafeGrainStorageInspectionResultService {

    @Autowired
    private SsSafeGrainStorageInspectionResultMapper ssSafeGrainStorageInspectionResultMapper;

    @Autowired
    private StorehouseInfoMapper storehouseInfoMapper;

    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;

    @Autowired
    private EnumMapper enumMapper;

    @Autowired
    private OrgInfoMapper orgInfoMapper;

    @Autowired
    private SsSafeGrainStorageInspectionMapper ssSafeGrainStorageInspectionMapper;

    @Autowired
    private DataKcglKcswDefaultMapper dataKcglKcswDefaultMapper;

    @Autowired
    private BasicEnumMapper basicEnumMapper;

    @Autowired
    private SsSafeProductInspectionMapper ssSafeProductInspectionMapper;

    @Override
    public Page<SsSafeGrainStorageInspectionResultVO> page(SsSafeGrainStorageInspectionResultPageParam pageParam) {

        List<SsSafeGrainStorageInspection> inspectionList = ssSafeGrainStorageInspectionResultMapper.
                selectMyPage(pageParam.getCurrent(), pageParam.getSize(), pageParam.getWarehouseName(), pageParam.getStorehouseName());

        ArrayList<SsSafeGrainStorageInspectionResultVO> list = new ArrayList<>();
        for (SsSafeGrainStorageInspection inspection : inspectionList) {

            SsSafeGrainStorageInspectionResultVO vo = new SsSafeGrainStorageInspectionResultVO();

            OrgInfo orgInfo = orgInfoMapper.selectById(inspection.getOrgId());
            if (ObjectUtil.isNotEmpty(orgInfo)) {
                vo.setOrgName(orgInfo.getOrgName());
            }
            vo.setSsSafeGrainStorageInspectionId(inspection.getId());

            SsSafeGrainStorageInspectionResult inspectionResult = ssSafeGrainStorageInspectionResultMapper.selectOne(Wrappers.lambdaQuery(SsSafeGrainStorageInspectionResult.class)
                    .eq(SsSafeGrainStorageInspectionResult::getSsSafeGrainStorageInspectionId, inspection.getId()));
            if (ObjectUtil.isNotEmpty(inspectionResult)) {
                BeanUtils.copyProperties(inspectionResult, vo);
                //仓房设值
                StorehouseInfo storehouseInfo = storehouseInfoMapper.selectById(inspectionResult.getBasicStorehouseId());
                if (ObjectUtil.isNotEmpty(storehouseInfo)) {
                    vo.setStorehouseName(storehouseInfo.getStorehouseName());
                }
                //货位设值
                WarehouseInfo warehouseInfo = warehouseInfoMapper.selectById(inspectionResult.getWarehouseId());
                if (ObjectUtil.isNotEmpty(warehouseInfo)) {
                    vo.setWarehouseName(warehouseInfo.getWarehouseName());
                }
                //粮食种类
                BasicEnum basicEnum = enumMapper.selectById(inspectionResult.getGrainVarietyId());
                if (ObjectUtil.isNotEmpty(basicEnum)) {
                    vo.setGrainVarietyName(basicEnum.getDictLabel());
                }
            } else {
                vo.setStorehouseName(null);
                vo.setWarehouseName(null);
                vo.setGrainGradeName(null);
            }
            list.add(vo);
        }

        Page<SsSafeGrainStorageInspectionResultVO> page = new Page<>();
        page.setRecords(list);
        page.setTotal(list.size());

        return page;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SsSafeGrainStorageInspectionResultAddParam ssSafeGrainStorageInspectionResultAddParam) {
        SsSafeGrainStorageInspectionResult ssSafeGrainStorageInspectionResult = BeanUtil.toBean(ssSafeGrainStorageInspectionResultAddParam, SsSafeGrainStorageInspectionResult.class);
        this.save(ssSafeGrainStorageInspectionResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SsSafeGrainStorageInspectionResultEditParam editParam) {

        SsSafeGrainStorageInspectionResult ssSafeGrainStorageInspectionResult = new SsSafeGrainStorageInspectionResult();
        BeanUtils.copyProperties(editParam, ssSafeGrainStorageInspectionResult);
        this.save(ssSafeGrainStorageInspectionResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SsSafeGrainStorageInspectionResultIdParam> ssSafeGrainStorageInspectionResultIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(ssSafeGrainStorageInspectionResultIdParamList, SsSafeGrainStorageInspectionResultIdParam::getSsSafeGrainStorageInspectionId));
    }

    @Override
    public SsSafeGrainStorageInspectionResultVO detail(SsSafeGrainStorageInspectionResultIdParam resultIdParam) {

        SsSafeGrainStorageInspectionResultVO vo = new SsSafeGrainStorageInspectionResultVO();

        SsSafeGrainStorageInspection ssSafeGrainStorageInspection = ssSafeGrainStorageInspectionMapper.selectById(resultIdParam.getSsSafeGrainStorageInspectionId());

        //设置库区名
        OrgInfo orgInfo = orgInfoMapper.selectById(ssSafeGrainStorageInspection.getOrgId());
        if (ObjectUtil.isNotEmpty(orgInfo)) {
            vo.setOrgName(orgInfo.getOrgName());
            vo.setOrgId(orgInfo.getOrgId());
        }

        SsSafeGrainStorageInspectionResult storageInspectionResult = ssSafeGrainStorageInspectionResultMapper.selectOne(Wrappers.lambdaQuery(SsSafeGrainStorageInspectionResult.class)
                .eq(SsSafeGrainStorageInspectionResult::getSsSafeGrainStorageInspectionId, resultIdParam.getSsSafeGrainStorageInspectionId()));
        if (ObjectUtil.isNotEmpty(storageInspectionResult)) {

            BeanUtils.copyProperties(storageInspectionResult, vo);
            //设置仓房名
            StorehouseInfo storehouseInfo = storehouseInfoMapper.selectById(storageInspectionResult.getBasicStorehouseId());
            if (ObjectUtil.isNotEmpty(storehouseInfo)) {
                vo.setStorehouseName(storehouseInfo.getStorehouseName());

            }

            //设置货位名
            WarehouseInfo warehouseInfo = warehouseInfoMapper.selectById(storageInspectionResult.getWarehouseId());
            if (ObjectUtil.isNotEmpty(warehouseInfo)) {
                vo.setWarehouseName(warehouseInfo.getWarehouseName());
            }

            //设置粮食种类
            BasicEnum grainVarietyName = enumMapper.selectById(storageInspectionResult.getGrainVarietyId());
            if (ObjectUtil.isNotEmpty(grainVarietyName)) {
                vo.setGrainVarietyName(grainVarietyName.getDictLabel());
            }

            //设置粮食等级
            BasicEnum grainGradeName = enumMapper.selectById(storageInspectionResult.getGrainGradeId());
            if (ObjectUtil.isNotEmpty(grainGradeName)) {
                vo.setGrainGradeName(grainGradeName.getDictLabel());
            }

        }
        return vo;
    }


    @Override
    public SsSafeGrainStorageInspectionResult queryEntity(String id) {
        SsSafeGrainStorageInspectionResult ssSafeGrainStorageInspectionResult = this.getById(id);
        if (ObjectUtil.isEmpty(ssSafeGrainStorageInspectionResult)) {
            throw new CommonException("安全仓储-安全储粮检查表-结果不存在，id值为：{}", id);
        }
        return ssSafeGrainStorageInspectionResult;
    }

    /**
     * 通过货位得到粮食信息-pad
     *
     * @author hjc
     * @date 2024/06/01 14:49
     */
    @Override
    public GrainInfoVO getGrain(Integer orgId, String warehouseId, String storehouseId) {

        GrainInfoVO vo = new GrainInfoVO();

        //返回安全储粮检查的种类和等级
        DataKcglKcswDefault dataKcglKcswDefault = dataKcglKcswDefaultMapper.selectOne(Wrappers.lambdaQuery(DataKcglKcswDefault.class)
                .eq(DataKcglKcswDefault::getUnitid, orgId)
                .eq(DataKcglKcswDefault::getCh, storehouseId)
                .eq(DataKcglKcswDefault::getHwh, warehouseId));
        if (ObjectUtil.isEmpty(dataKcglKcswDefault)) {
            return null;
        }
        //粮食等级
        String djId = dataKcglKcswDefault.getDj();
        com.unis.basic.info.modular.enumInfo.entity.BasicEnum djName = basicEnumMapper.selectById(djId);
        vo.setGrainGradeName(djName.getEnumname());
        vo.setGrainGradeId(djName.getEnumid());

        //粮食种类
        String pzId = dataKcglKcswDefault.getPz();
        com.unis.basic.info.modular.enumInfo.entity.BasicEnum pzName = basicEnumMapper.selectById(pzId);
        vo.setGrainVarietyName(pzName.getEnumname());
        vo.setGrainVarietyId(pzName.getEnumid());

        //返回安全生产检查的数据

        return vo;

    }

    /**
     * 获取安全仓储-安全储粮检查表-结果分页-省平台
     *
     * @author hjc
     * @date 2024/06/03 10:48
     */
    @Override
    public Page<SsSafeGrainStorageInspectionResultVO> getResultPage(ResultPageParam pageParam) {

        Page<SsSafeGrainStorageInspectionResultVO> page = new Page<>();
        return ssSafeGrainStorageInspectionResultMapper.selectMyResultPage(page, pageParam.getStorehouseName(),
                pageParam.getOrgName(), DateUtil.format(pageParam.getInspectionDate(), "yyyy-MM-dd"));

    }
}
