/*
 * 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.bizPssMoGrain.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.bizPssMoGrain.param.*;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoInventoryTimeAnomaly.entity.BizPssMoInventoryTimeAnomaly;
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.bizPssMoGrain.entity.BizPssMoGrain;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoGrain.mapper.BizPssMoGrainMapper;
import com.unis.purchaseAndSalesSupervision.modular.bizPssMoGrain.service.BizPssMoGrainService;

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

/**
 * 模型优化-粮情模型优化表Service接口实现类
 *
 * @author hjc
 * @date 2024/06/12 19:59
 **/
@Service
public class BizPssMoGrainServiceImpl extends ServiceImpl<BizPssMoGrainMapper, BizPssMoGrain> implements BizPssMoGrainService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BizPssMoGrainMapper bizPssMoGrainMapper;

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizPssMoGrainAddParam bizPssMoGrainAddParam) {
        List<BizPssMoGrain> bizPssMoInventoryTimeAnomalies = bizPssMoGrainMapper.selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                .eq(BizPssMoGrain::getModelName, bizPssMoGrainAddParam.getModelName()));
        if (CollectionUtil.isNotEmpty(bizPssMoInventoryTimeAnomalies)) {
            throw new CommonException("模型名称已存在，请重新输入");
        }
        List<BizPssMoGrain> bizPssMoInventoryTimeAnomalyList = bizPssMoGrainMapper
                .selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                        .eq(BizPssMoGrain::getModelNumber, bizPssMoGrainAddParam.getModelNumber()));
        if (CollectionUtil.isNotEmpty(bizPssMoInventoryTimeAnomalyList)) {
            throw new CommonException("模型编号已存在，请重新输入");
        }

        if (bizPssMoGrainAddParam.getIsEnabled() == 1) {
            //开启的话，将其他的设置为关0，自身id设置为开1
            List<BizPssMoGrain> bizPssMoGrainList = bizPssMoGrainMapper
                    .selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                            .eq(BizPssMoGrain::getIsEnabled, 1));
            if (CollectionUtil.isNotEmpty(bizPssMoGrainList)) {
                for (BizPssMoGrain bizPssMoGrain1 : bizPssMoGrainList) {
                    bizPssMoGrain1.setIsEnabled(0);
                    bizPssMoGrainMapper.updateById(bizPssMoGrain1);
                }
            }
        }
        BizPssMoGrain bizPssMoGrain = BeanUtil.toBean(bizPssMoGrainAddParam, BizPssMoGrain.class);
        this.save(bizPssMoGrain);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizPssMoGrainEditParam bizPssMoGrainEditParam) {

        List<BizPssMoGrain> bizPssMoInventoryTimeAnomalies = bizPssMoGrainMapper.selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                .eq(BizPssMoGrain::getModelName, bizPssMoGrainEditParam.getModelName()));
        if (CollectionUtil.isNotEmpty(bizPssMoInventoryTimeAnomalies) && !Objects.equals(bizPssMoInventoryTimeAnomalies.get(0).getId(),
                bizPssMoGrainEditParam.getId())) {
            throw new CommonException("模型名称已存在，请重新输入");
        }
        List<BizPssMoGrain> bizPssMoInventoryTimeAnomalyList = bizPssMoGrainMapper
                .selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                        .eq(BizPssMoGrain::getModelNumber, bizPssMoGrainEditParam.getModelNumber()));
        if (CollectionUtil.isNotEmpty(bizPssMoInventoryTimeAnomalyList) && !Objects.equals(bizPssMoInventoryTimeAnomalies.get(0).getId(),
                bizPssMoGrainEditParam.getId())) {
            throw new CommonException("模型编号已存在，请重新输入");
        }

        if (bizPssMoGrainEditParam.getIsEnabled() == 1) {
            //开启的话，将其他的设置为关0，自身id设置为开1
            List<BizPssMoGrain> bizPssMoGrainList = bizPssMoGrainMapper
                    .selectList(Wrappers.lambdaQuery(BizPssMoGrain.class)
                            .eq(BizPssMoGrain::getIsEnabled, 1));
            if (CollectionUtil.isNotEmpty(bizPssMoGrainList)) {
                for (BizPssMoGrain bizPssMoGrain1 : bizPssMoGrainList) {
                    bizPssMoGrain1.setIsEnabled(0);
                    bizPssMoGrainMapper.updateById(bizPssMoGrain1);
                }
            }
        }
        BizPssMoGrain bizPssMoGrain = this.queryEntity(bizPssMoGrainEditParam.getId());
        BeanUtil.copyProperties(bizPssMoGrainEditParam, bizPssMoGrain);
        this.updateById(bizPssMoGrain);

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

    @Override
    public BizPssMoGrain detail(BizPssMoGrainIdParam bizPssMoGrainIdParam) {
        BizPssMoGrain bizPssMoGrain = this.queryEntity(bizPssMoGrainIdParam.getId());
        SysUser sysUser = sysUserMapper.selectById(bizPssMoGrain.getCreateUser());
        if (ObjectUtil.isNotEmpty(sysUser)){
            bizPssMoGrain.setCreateUser(sysUser.getName());
        }
        return bizPssMoGrain;
    }

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

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

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

        }

    }
}
