/*
 * 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.mobileInfo.modular.qcqualityinspection.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.basic.info.modular.enumInfo.entity.BasicEnum;
import com.unis.basic.info.modular.enumInfo.mapper.BasicEnumMapper;
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.mobileInfo.modular.qcqualityinspection.param.BusinessQcQualityInspectionAddParam;
import com.unis.mobileInfo.modular.qcqualityinspection.param.BusinessQcQualityInspectionEditParam;
import com.unis.mobileInfo.modular.qcqualityinspection.param.BusinessQcQualityInspectionIdParam;
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.mobileInfo.modular.qcqualityinspection.entity.BusinessQcQualityInspection;
import com.unis.mobileInfo.modular.qcqualityinspection.mapper.BusinessQcQualityInspectionMapper;
import com.unis.mobileInfo.modular.qcqualityinspection.param.BusinessQcQualityInspectionPageParam;
import com.unis.mobileInfo.modular.qcqualityinspection.service.BusinessQcQualityInspectionService;

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

/**
 * 质量管理-验收质检表Service接口实现类
 *
 * @author shine
 * @date  2024/06/06 11:25
 **/
@Service
public class BusinessQcQualityInspectionServiceImpl extends ServiceImpl<BusinessQcQualityInspectionMapper, BusinessQcQualityInspection> implements BusinessQcQualityInspectionService {

    @Resource
    private StorehouseInfoMapper storehouseInfoMapper;

    @Resource
    private WarehouseInfoMapper warehouseInfoMapper;

    @Resource
    private BasicEnumMapper basicEnumMapper;
    @Override
    public Page<BusinessQcQualityInspection> page(BusinessQcQualityInspectionPageParam businessQcQualityInspectionPageParam) {
        QueryWrapper<BusinessQcQualityInspection> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getOrgId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getOrgId, businessQcQualityInspectionPageParam.getOrgId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getBasicStorehouseId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getBasicStorehouseId, businessQcQualityInspectionPageParam.getBasicStorehouseId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getWarehouseId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getWarehouseId, businessQcQualityInspectionPageParam.getWarehouseId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getStartJysj()) && ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getEndJysj())) {
            queryWrapper.lambda().between(BusinessQcQualityInspection::getJysj, businessQcQualityInspectionPageParam.getStartJysj(), businessQcQualityInspectionPageParam.getEndJysj());
        }
        if(ObjectUtil.isAllNotEmpty(businessQcQualityInspectionPageParam.getSortField(), businessQcQualityInspectionPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(businessQcQualityInspectionPageParam.getSortOrder());
            queryWrapper.orderBy(true, businessQcQualityInspectionPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(businessQcQualityInspectionPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BusinessQcQualityInspection::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BusinessQcQualityInspectionAddParam businessQcQualityInspectionAddParam) {
        BusinessQcQualityInspection businessQcQualityInspection = BeanUtil.toBean(businessQcQualityInspectionAddParam, BusinessQcQualityInspection.class);
        this.save(businessQcQualityInspection);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BusinessQcQualityInspectionEditParam businessQcQualityInspectionEditParam) {
        BusinessQcQualityInspection businessQcQualityInspection = this.queryEntity(businessQcQualityInspectionEditParam.getId().toString());
        BeanUtil.copyProperties(businessQcQualityInspectionEditParam, businessQcQualityInspection);
        this.updateById(businessQcQualityInspection);
    }

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

    @Override
    public BusinessQcQualityInspection detail(BusinessQcQualityInspectionIdParam businessQcQualityInspectionIdParam) {
        return this.queryEntity(businessQcQualityInspectionIdParam.getId().toString());
    }

    @Override
    public BusinessQcQualityInspection queryEntity(String id) {
        BusinessQcQualityInspection businessQcQualityInspection = this.getById(id);
        if(ObjectUtil.isEmpty(businessQcQualityInspection)) {
            throw new CommonException("质量管理-验收质检表不存在，id值为：{}", id);
        }
        return businessQcQualityInspection;
    }

    @Override
    public List<BusinessQcQualityInspection> getData(BusinessQcQualityInspectionPageParam businessQcQualityInspectionPageParam) {
        QueryWrapper<BusinessQcQualityInspection> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getOrgId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getOrgId, businessQcQualityInspectionPageParam.getOrgId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getBasicStorehouseId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getBasicStorehouseId, businessQcQualityInspectionPageParam.getBasicStorehouseId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getWarehouseId())) {
            queryWrapper.lambda().eq(BusinessQcQualityInspection::getWarehouseId, businessQcQualityInspectionPageParam.getWarehouseId());
        }
        if(ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getStartJysj()) && ObjectUtil.isNotEmpty(businessQcQualityInspectionPageParam.getEndJysj())) {
            queryWrapper.lambda().between(BusinessQcQualityInspection::getJysj, businessQcQualityInspectionPageParam.getStartJysj(), businessQcQualityInspectionPageParam.getEndJysj());
        }
        if(ObjectUtil.isAllNotEmpty(businessQcQualityInspectionPageParam.getSortField(), businessQcQualityInspectionPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(businessQcQualityInspectionPageParam.getSortOrder());
            queryWrapper.orderBy(true, businessQcQualityInspectionPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(businessQcQualityInspectionPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BusinessQcQualityInspection::getId);
        }

        List<BusinessQcQualityInspection> list = this.list(queryWrapper);
        for (BusinessQcQualityInspection businessQcQualityInspection:list) {
            StorehouseInfo storehouseInfo = storehouseInfoMapper.selectById(businessQcQualityInspection.getBasicStorehouseId());
            WarehouseInfo warehouseInfo = warehouseInfoMapper.selectById(businessQcQualityInspection.getWarehouseId());
            BasicEnum basicEnum = basicEnumMapper.selectById(businessQcQualityInspection.getGrainVarietyId());
            if(ObjectUtil.isNotNull(storehouseInfo)){
                businessQcQualityInspection.setStorehouseName(storehouseInfo.getStorehouseName());
            }
            if(ObjectUtil.isNotNull(warehouseInfo)){
                businessQcQualityInspection.setWarehouseName(warehouseInfo.getWarehouseName());
            }
            if(ObjectUtil.isNotNull(basicEnum)){
                businessQcQualityInspection.setGrainVarietyName(basicEnum.getEnumname());
            }
        }
        return list;
    }
}
