/*
 * 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.statisticalReport.info.modular.ssSafeProductInspectionResult.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
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.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.warehouseInfo.entity.WarehouseInfo;
import com.unis.basic.info.modular.warehouseInfo.mapper.WarehouseInfoMapper;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.vo.SsSafeProductInspectionResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.exception.CommonException;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.entity.SsSafeProductInspectionResult;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.mapper.SsSafeProductInspectionResultMapper;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.param.SsSafeProductInspectionResultAddParam;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.param.SsSafeProductInspectionResultEditParam;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.param.SsSafeProductInspectionResultIdParam;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.param.SsSafeProductInspectionResultPageParam;
import com.unis.statisticalReport.info.modular.ssSafeProductInspectionResult.service.SsSafeProductInspectionResultService;

import java.util.List;

/**
 * 日常安全检查-安全生产检查-结果表Service接口实现类
 *
 * @author hjc
 * @date 2024/06/01 14:49
 **/
@Service
public class SsSafeProductInspectionResultServiceImpl extends ServiceImpl<SsSafeProductInspectionResultMapper, SsSafeProductInspectionResult> implements SsSafeProductInspectionResultService {

    @Autowired
    private SsSafeProductInspectionResultMapper ssSafeProductInspectionResultMapper;

    @Autowired
    private OrgInfoMapper orgInfoMapper;

    @Autowired
    private StorehouseInfoMapper storehouseInfoMapper;

    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;

    @Override
    public Page<SsSafeProductInspectionResultVO> page(SsSafeProductInspectionResultPageParam pageParam) {
      /*  QueryWrapper<SsSafeProductInspectionResult> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isAllNotEmpty(ssSafeProductInspectionResultPageParam.getSortField(), ssSafeProductInspectionResultPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(ssSafeProductInspectionResultPageParam.getSortOrder());
            queryWrapper.orderBy(true, ssSafeProductInspectionResultPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(ssSafeProductInspectionResultPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SsSafeProductInspectionResult::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);*/

        Page<SsSafeProductInspectionResultVO> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());

        return ssSafeProductInspectionResultMapper.selectMyPage(page, pageParam.getWarehouseName(), pageParam.getStorehouseName());

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SsSafeProductInspectionResultAddParam ssSafeProductInspectionResultAddParam) {
        SsSafeProductInspectionResult ssSafeProductInspectionResult = BeanUtil.toBean(ssSafeProductInspectionResultAddParam, SsSafeProductInspectionResult.class);
        this.save(ssSafeProductInspectionResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SsSafeProductInspectionResultEditParam editParam) {
      /*  SsSafeProductInspectionResult ssSafeProductInspectionResult = this.queryEntity(ssSafeProductInspectionResultEditParam.getId());
        BeanUtil.copyProperties(ssSafeProductInspectionResultEditParam, ssSafeProductInspectionResult);
        this.updateById(ssSafeProductInspectionResult);*/

        SsSafeProductInspectionResult ssSafeProductInspectionResult = new SsSafeProductInspectionResult();
        BeanUtil.copyProperties(editParam, ssSafeProductInspectionResult);

        this.save(ssSafeProductInspectionResult);
    }

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

    @Override
    public SsSafeProductInspectionResultVO detail(SsSafeProductInspectionResultIdParam resultIdParam) {

        SsSafeProductInspectionResultVO vo = new SsSafeProductInspectionResultVO();

        //库区设值
        OrgInfo orgInfo = orgInfoMapper.selectOrgInfoBySsSafeProductInspectionId(resultIdParam.getSsSafeProductInspectionId());
        if (ObjectUtil.isNotEmpty(orgInfo)) {
            vo.setOrgId(orgInfo.getOrgId());
            vo.setOrgName(orgInfo.getOrgName());
        }

        SsSafeProductInspectionResult ssSafeProductInspectionResult = ssSafeProductInspectionResultMapper.selectOne(Wrappers.lambdaQuery(SsSafeProductInspectionResult.class)
                .eq(SsSafeProductInspectionResult::getSsSafeProductInspectionId, resultIdParam.getSsSafeProductInspectionId()));
        if (ObjectUtil.isNotEmpty(ssSafeProductInspectionResult)) {
            BeanUtil.copyProperties(ssSafeProductInspectionResult, vo);

            //仓房设值
            StorehouseInfo storehouseInfo = storehouseInfoMapper.selectOne(Wrappers.lambdaQuery(StorehouseInfo.class)
                    .eq(StorehouseInfo::getStorehouseId, ssSafeProductInspectionResult.getBasicStorehouseId()));
            vo.setStorehouseName(storehouseInfo.getStorehouseName());

            //货位设值
            WarehouseInfo warehouseInfo = warehouseInfoMapper.selectOne(Wrappers.lambdaQuery(WarehouseInfo.class)
                    .eq(WarehouseInfo::getWarehouseId, ssSafeProductInspectionResult.getWarehouseId()));
            vo.setWarehouseName(warehouseInfo.getWarehouseName());

        } else {
            vo.setWarehouseName(null);
            vo.setStorehouseName(null);
        }
        return vo;
    }

    @Override
    public SsSafeProductInspectionResult queryEntity(String id) {
        SsSafeProductInspectionResult ssSafeProductInspectionResult = this.getById(id);
        if (ObjectUtil.isEmpty(ssSafeProductInspectionResult)) {
            throw new CommonException("日常安全检查-安全生产检查-结果表不存在，id值为：{}", id);
        }
        return ssSafeProductInspectionResult;
    }


    /**
     * 通过库区id获取仓房信息
     *
     * @author hjc
     * @date 2024/06/01 14:49
     */
    @Override
    public List<StorehouseInfo> getStorehouse(Integer orgId) {
        return storehouseInfoMapper.selectList(Wrappers.lambdaQuery(StorehouseInfo.class)
                .eq(StorehouseInfo::getOrgId, orgId));
    }

    /**
     * 通过仓房id获取货位信息
     *
     * @author hjc
     * @date 2024/06/01 14:49
     */
    @Override
    public List<WarehouseInfo> getWarehouse(String storehouseId) {
        return warehouseInfoMapper.selectList(Wrappers.lambdaQuery(WarehouseInfo.class)
                .eq(WarehouseInfo::getStorehouseId, storehouseId));
    }
}
