package com.jwipc.spe.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.security.dto.CurrentUserDTO;
import com.jwipc.spe.convert.SupAppraiseDimensionConvert;
import com.jwipc.spe.dto.SupAppraiseDimensionDTO;
import com.jwipc.spe.entity.SupAppraiseDimension;
import com.jwipc.spe.logic.SupAppraiseDimensionLogic;
import com.jwipc.spe.model.po.SupAppraiseDimensionPO;
import com.jwipc.spe.model.query.SupAppraiseDimensionQuery;
import com.jwipc.spe.service.ISupAppraiseDimensionService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author Al
 * @create 2024-02-28 16:08
 * 考核维度
 */
@Service
@RequiredArgsConstructor
public class SupAppraiseDimensionLogicImpl implements SupAppraiseDimensionLogic {
    private final ISupAppraiseDimensionService supAppraiseDimensionService;
    private final SupAppraiseDimensionConvert supAppraiseDimensionConvert;

    //列表页面数据
    @Override
    public PageView findByPage(SupAppraiseDimensionQuery query) {
        final Page<SupAppraiseDimension> dbPage = PageUtil.toPage(query);

        List<SupAppraiseDimensionPO> supAppraiseDimensionPOS = supAppraiseDimensionService.findPageByQuery(dbPage,
                query);

        return PageUtil.getPV(supAppraiseDimensionConvert.po2PageListDTO(supAppraiseDimensionPOS), dbPage.getTotal());
    }

    //列表主数据
    @Override
    public List<SupAppraiseDimensionDTO> listAll(SupAppraiseDimensionQuery query) {
        List<SupAppraiseDimensionPO> supAppraiseDimensionPOS = supAppraiseDimensionService.findPageByQuery(null, query);
        return supAppraiseDimensionConvert.po2PageListDTO(supAppraiseDimensionPOS);
    }

    //新增编辑的保存
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(SupAppraiseDimensionDTO param) {
        //保存
        if (Objects.isNull(param.getId())) {
            final LambdaQueryWrapper<SupAppraiseDimension> resourcesLambdaQueryWrapper =
                    new LambdaQueryWrapper<SupAppraiseDimension>().eq(
                            SupAppraiseDimension::getDimensionCode, param.getDimensionCode());
            if (ObjectUtil.isNotNull(supAppraiseDimensionService.getOne(resourcesLambdaQueryWrapper, false))) {
                throw new DaoException("维度编码重复");
            }

            param.setCreateTime(LocalDateTime.now());
            param.setCreateUserId(LoginUtil.getUserId());
            param.setCreateUserName(LoginUtil.getCurrentUser().getName());
        } else {
            final LambdaQueryWrapper<SupAppraiseDimension> resourcesLambdaQueryWrapper =
                    new LambdaQueryWrapper<SupAppraiseDimension>().eq(
                                    SupAppraiseDimension::getDimensionCode, param.getDimensionCode())
                            .ne(SupAppraiseDimension::getId, param.getId());
            if (ObjectUtil.isNotNull(supAppraiseDimensionService.getOne(resourcesLambdaQueryWrapper, false))) {
                throw new DaoException("维度编码重复");
            }
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUserId(LoginUtil.getUserId());
            param.setUpdateUserName(LoginUtil.getCurrentUser().getName());
        }
        supAppraiseDimensionService.saveOrUpdate(supAppraiseDimensionConvert.param2Entity(param));
    }

    /**
     * 启用或停用维度指标数据
     *
     * @param ids    数据Ids
     * @param status 1:启用 2:停用
     * @param user   当前操作人
     * @return 结果
     */
    @Override
    public boolean invalidOrEffectiveTarget(String ids, Integer status, CurrentUserDTO user) {
        boolean isOk = false;
        if (StringUtils.isNotEmpty(ids)) {
            List<SupAppraiseDimension> list = Lists.newArrayList();
            for (String id : StringUtils.split(ids, ",")) {
                SupAppraiseDimension sat = new SupAppraiseDimension();
                sat.setId(Long.valueOf(id));
                sat.setStatus(status);
                sat.setUpdateTime(LocalDateTime.now());
                sat.setUpdateUserId(LoginUtil.getUserId());
                sat.setUpdateUserName(LoginUtil.getName());

                list.add(sat);
            }
            if (CollectionUtil.isNotEmpty(list)) {
                isOk = supAppraiseDimensionService.updateBatchById(list);
                if (!isOk) {
                    throw new DaoException("操作数据失败!");
                }
            }

            return true;
        }
        return false;
    }

    //根据id查询数据
    @Override
    public SupAppraiseDimensionDTO getById(Integer id) {
        if (id != null) {
            SupAppraiseDimensionPO supAppraiseDimensionPO = supAppraiseDimensionService.getDataById(id);
            return supAppraiseDimensionConvert.po2DTO(supAppraiseDimensionPO);
        } else {
            return new SupAppraiseDimensionDTO();
        }
    }
}
