/*
 * 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.purchaseAndSalesSupervision.modular.bizPssMoRotationEmptyPeriod.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.purchaseAndSalesSupervision.modular.bizPssMoInventoryTimeAnomaly.entity.BizPssMoInventoryTimeAnomaly;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoRotationEmptyPeriod.param.*;
import com.unis.sys.modular.user.entity.SysUser;
import com.unis.sys.modular.user.mapper.SysUserMapper;
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.purchaseAndSalesSupervision.modular.bizPssMoRotationEmptyPeriod.entity.BizPssMoRotationEmptyPeriod;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoRotationEmptyPeriod.mapper.BizPssMoRotationEmptyPeriodMapper;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoRotationEmptyPeriod.service.BizPssMoRotationEmptyPeriodService;

import java.util.List;
import java.util.Objects;

/**
 * 模型优化-轮空期模型优化表Service接口实现类
 *
 * @author hjc
 * @date 2024/06/12 20:03
 **/
@Service
public class BizPssMoRotationEmptyPeriodServiceImpl extends ServiceImpl<BizPssMoRotationEmptyPeriodMapper, BizPssMoRotationEmptyPeriod> implements BizPssMoRotationEmptyPeriodService {

    @Autowired
    private BizPssMoRotationEmptyPeriodMapper bizPssMoRotationEmptyPeriodMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public Page<BizPssMoRotationEmptyPeriod> page(BizPssMoRotationEmptyPeriodPageParam bizPssMoRotationEmptyPeriodPageParam) {
        QueryWrapper<BizPssMoRotationEmptyPeriod> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bizPssMoRotationEmptyPeriodPageParam.getModelName())) {
            queryWrapper.lambda().like(BizPssMoRotationEmptyPeriod::getModelName, bizPssMoRotationEmptyPeriodPageParam.getModelName());
        }
        if (ObjectUtil.isAllNotEmpty(bizPssMoRotationEmptyPeriodPageParam.getSortField(), bizPssMoRotationEmptyPeriodPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizPssMoRotationEmptyPeriodPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizPssMoRotationEmptyPeriodPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizPssMoRotationEmptyPeriodPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(BizPssMoRotationEmptyPeriod::getCreateTime);
        }
        Page<BizPssMoRotationEmptyPeriod> page = this.page(CommonPageRequest.defaultPage(), queryWrapper);
        List<BizPssMoRotationEmptyPeriod> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            for (BizPssMoRotationEmptyPeriod bizPssMoRotationEmptyPeriod : records) {
                SysUser sysUser = sysUserMapper.selectById(bizPssMoRotationEmptyPeriod.getCreateUser());
                if (ObjectUtil.isNotEmpty(sysUser)) {
                    bizPssMoRotationEmptyPeriod.setCreateUser(sysUser.getName());
                }
            }
        }
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizPssMoRotationEmptyPeriodAddParam bizPssMoRotationEmptyPeriodAddParam) {

        List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriods = bizPssMoRotationEmptyPeriodMapper.selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                .eq(BizPssMoRotationEmptyPeriod::getModelName, bizPssMoRotationEmptyPeriodAddParam.getModelName()));
        if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriods)) {
            throw new CommonException("模型名称已存在，请重新输入");
        }
        List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriodList = bizPssMoRotationEmptyPeriodMapper
                .selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                        .eq(BizPssMoRotationEmptyPeriod::getModelNumber, bizPssMoRotationEmptyPeriodAddParam.getModelNumber()));
        if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriodList)) {
            throw new CommonException("模型编号已存在，请重新输入");
        }
        if (bizPssMoRotationEmptyPeriodAddParam.getIsEnabled()==1){
            //开启的话，将其他的设置为关0，自身id设置为开1
            List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriodList1 = bizPssMoRotationEmptyPeriodMapper.selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                    .eq(BizPssMoRotationEmptyPeriod::getIsEnabled, 1));
            if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriodList1)){
                for (BizPssMoRotationEmptyPeriod pssMoRotationEmptyPeriod : bizPssMoRotationEmptyPeriodList1) {
                    pssMoRotationEmptyPeriod.setIsEnabled(0);
                    bizPssMoRotationEmptyPeriodMapper.updateById(pssMoRotationEmptyPeriod);
                }
            }
        }
        BizPssMoRotationEmptyPeriod bizPssMoRotationEmptyPeriod = BeanUtil.toBean(bizPssMoRotationEmptyPeriodAddParam, BizPssMoRotationEmptyPeriod.class);
        this.save(bizPssMoRotationEmptyPeriod);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizPssMoRotationEmptyPeriodEditParam bizPssMoRotationEmptyPeriodEditParam) {
        List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriods = bizPssMoRotationEmptyPeriodMapper
                .selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                        .eq(BizPssMoRotationEmptyPeriod::getModelName, bizPssMoRotationEmptyPeriodEditParam.getModelName()));
        if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriods) && !Objects.equals(bizPssMoRotationEmptyPeriods.get(0).getId(),
                bizPssMoRotationEmptyPeriodEditParam.getId())) {
            throw new CommonException("模型名称已存在，请重新输入");
        }
        List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriodList = bizPssMoRotationEmptyPeriodMapper
                .selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                        .eq(BizPssMoRotationEmptyPeriod::getModelNumber, bizPssMoRotationEmptyPeriodEditParam.getModelNumber()));
        if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriodList) && !Objects.equals(bizPssMoRotationEmptyPeriods.get(0).getId(),
                bizPssMoRotationEmptyPeriodEditParam.getId())) {
            throw new CommonException("模型编号已存在，请重新输入");
        }
        if (bizPssMoRotationEmptyPeriodEditParam.getIsEnabled()==1){
            //开启的话，将其他的设置为关0，自身id设置为开1
            List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriodList1 = bizPssMoRotationEmptyPeriodMapper.selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                    .eq(BizPssMoRotationEmptyPeriod::getIsEnabled, 1));
            if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriodList1)){
                for (BizPssMoRotationEmptyPeriod pssMoRotationEmptyPeriod : bizPssMoRotationEmptyPeriodList1) {
                    pssMoRotationEmptyPeriod.setIsEnabled(0);
                    bizPssMoRotationEmptyPeriodMapper.updateById(pssMoRotationEmptyPeriod);
                }
            }
        }
        BizPssMoRotationEmptyPeriod bizPssMoRotationEmptyPeriod = this.queryEntity(bizPssMoRotationEmptyPeriodEditParam.getId());
        BeanUtil.copyProperties(bizPssMoRotationEmptyPeriodEditParam, bizPssMoRotationEmptyPeriod);
        this.updateById(bizPssMoRotationEmptyPeriod);
    }

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

    @Override
    public BizPssMoRotationEmptyPeriod detail(BizPssMoRotationEmptyPeriodIdParam bizPssMoRotationEmptyPeriodIdParam) {
        return this.queryEntity(bizPssMoRotationEmptyPeriodIdParam.getId());
    }

    @Override
    public BizPssMoRotationEmptyPeriod queryEntity(String id) {
        BizPssMoRotationEmptyPeriod bizPssMoRotationEmptyPeriod = this.getById(id);
        if (ObjectUtil.isEmpty(bizPssMoRotationEmptyPeriod)) {
            throw new CommonException("模型优化-轮空期模型优化表不存在，id值为：{}", id);
        }
        return bizPssMoRotationEmptyPeriod;
    }

    /**
     * 获取模型优化-轮空期模型优化表-开启关闭
     *
     * @author hjc
     * @date  2024/06/12 20:03
     */
    @Override
    public void isEnabledById(BizPssMoRotationEmptyPeriodIsEnabledByIdParam isEnabledByIdParam) {

        BizPssMoRotationEmptyPeriod bizPssMoRotationEmptyPeriod = this.queryEntity(isEnabledByIdParam.getId());
        if (isEnabledByIdParam.getIsEnabled()==0){
            //关闭，直接设置为0
            bizPssMoRotationEmptyPeriod.setIsEnabled(0);
            bizPssMoRotationEmptyPeriodMapper.updateById(bizPssMoRotationEmptyPeriod);
        }
        if (isEnabledByIdParam.getIsEnabled()==1){
            //开启的话，将其他的设置为关0，自身id设置为开1
            List<BizPssMoRotationEmptyPeriod> bizPssMoRotationEmptyPeriodList = bizPssMoRotationEmptyPeriodMapper.selectList(Wrappers.lambdaQuery(BizPssMoRotationEmptyPeriod.class)
                    .eq(BizPssMoRotationEmptyPeriod::getIsEnabled, 1));
            if (CollectionUtil.isNotEmpty(bizPssMoRotationEmptyPeriodList)){
                for (BizPssMoRotationEmptyPeriod pssMoRotationEmptyPeriod : bizPssMoRotationEmptyPeriodList) {
                    pssMoRotationEmptyPeriod.setIsEnabled(0);
                    bizPssMoRotationEmptyPeriodMapper.updateById(pssMoRotationEmptyPeriod);
                }
            }
            bizPssMoRotationEmptyPeriod.setIsEnabled(isEnabledByIdParam.getIsEnabled());
            bizPssMoRotationEmptyPeriodMapper.updateById(bizPssMoRotationEmptyPeriod);

        }

    }
}
