package com.cy.guoyan.admin.module.guoyan.service.stateownedreformlawlevel;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.cy.guoyan.admin.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cy.guoyan.admin.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import com.cy.guoyan.admin.module.guoyan.controller.admin.stateownedreformlawlevel.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.stateownedreformlawlevel.StateOwnedReformLawLevelDO;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import com.cy.guoyan.admin.module.guoyan.dal.mysql.stateownedreformlawlevel.StateOwnedReformLawLevelMapper;
import static com.cy.guoyan.admin.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cy.guoyan.admin.module.system.enums.ErrorCodeConstants.*;

/**
 * 国企改革法规层级 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StateOwnedReformLawLevelServiceImpl implements StateOwnedReformLawLevelService {

    @Resource
    private StateOwnedReformLawLevelMapper stateOwnedReformLawLevelMapper;

    @Override
    public Long createStateOwnedReformLawLevel(StateOwnedReformLawLevelSaveReqVO createReqVO) {
        StateOwnedReformLawLevelDO categoryDO = stateOwnedReformLawLevelMapper.selectOne(
                new LambdaQueryWrapperX<StateOwnedReformLawLevelDO>()
                        .orderByDesc(StateOwnedReformLawLevelDO::getLevelCode)
                        .last("LIMIT 1")
        );
        Integer levelCode = categoryDO.getLevelCode();
        levelCode++;
        String nickname = String.valueOf(SecurityFrameworkUtils.getLoginUser().getInfo().get("nickname"));
        createReqVO.setStatus("正常");
        // 插入
        StateOwnedReformLawLevelDO stateOwnedReformLawLevel = BeanUtils.toBean(createReqVO, StateOwnedReformLawLevelDO.class);
        stateOwnedReformLawLevel.setCreatorName(nickname);
        DefaultIdentifierGenerator generator = new DefaultIdentifierGenerator();
        stateOwnedReformLawLevel.setId(generator.nextId(null));
        stateOwnedReformLawLevel.setLevelCode(levelCode);
        stateOwnedReformLawLevelMapper.insert(stateOwnedReformLawLevel);
        // 返回
        return stateOwnedReformLawLevel.getId();
    }

    @Override
    public void updateStateOwnedReformLawLevel(StateOwnedReformLawLevelSaveReqVO updateReqVO) {
        // 校验存在
        validateStateOwnedReformLawLevelExists(updateReqVO.getId());
        // 更新
        StateOwnedReformLawLevelDO updateObj = BeanUtils.toBean(updateReqVO, StateOwnedReformLawLevelDO.class);
        stateOwnedReformLawLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteStateOwnedReformLawLevel(Long id) {
        // 校验存在
        validateStateOwnedReformLawLevelExists(id);
        // 删除
        stateOwnedReformLawLevelMapper.deleteById(id);
    }

    @Override
        public void deleteStateOwnedReformLawLevelListByIds(List<Long> ids) {
        // 校验存在
        validateStateOwnedReformLawLevelExists(ids);
        // 删除
        stateOwnedReformLawLevelMapper.deleteByIds(ids);
        }

    private void validateStateOwnedReformLawLevelExists(List<Long> ids) {
        List<StateOwnedReformLawLevelDO> list = stateOwnedReformLawLevelMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(STATE_OWNED_REFORM_LAW_LEVEL_NOT_EXISTS);
        }
    }

    private void validateStateOwnedReformLawLevelExists(Long id) {
        if (stateOwnedReformLawLevelMapper.selectById(id) == null) {
            throw exception(STATE_OWNED_REFORM_LAW_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public StateOwnedReformLawLevelDO getStateOwnedReformLawLevel(Long id) {
        return stateOwnedReformLawLevelMapper.selectById(id);
    }

    @Override
    public PageResult<StateOwnedReformLawLevelDO> getStateOwnedReformLawLevelPage(StateOwnedReformLawLevelPageReqVO pageReqVO) {
        return stateOwnedReformLawLevelMapper.selectPage(pageReqVO);
    }
    @Override
    public List<StateOwnedReformLawLevelDO> getStateOwnedReformLawLevelList(StateOwnedReformLawLevelPageReqVO pageReqVO) {
        pageReqVO.setPageSize(-1);
        return stateOwnedReformLawLevelMapper.selectPage(pageReqVO).getList();
    }

    @Override
    public void voidStateOwnedReformLawLevel(Long id, String status) {
        StateOwnedReformLawLevelDO infoDO = stateOwnedReformLawLevelMapper.selectById(id);
        if (infoDO == null) {
            throw exception(STATE_OWNED_REFORM_LAW_LEVEL_NOT_EXISTS);
        }
        infoDO.setStatus(status);
        stateOwnedReformLawLevelMapper.updateById(infoDO);
    }

}