package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.DealtTypeConvert;
import com.jwipc.sys.convert.SapLogConvert;
import com.jwipc.sys.dto.DealtWithTargetTypeDTO;
import com.jwipc.sys.entity.DealtWithTargetType;
import com.jwipc.sys.entity.Factory;
import com.jwipc.sys.entity.FactoryCompany;
import com.jwipc.sys.entity.UserLogin;
import com.jwipc.sys.logic.DealtTypeLogic;
import com.jwipc.sys.model.po.DealtWithTargetTypePO;
import com.jwipc.sys.model.po.SapLogInfoPO;
import com.jwipc.sys.model.query.DealtTypeQuery;
import com.jwipc.sys.service.IDealtWithTargetInfoService;
import com.jwipc.sys.service.IDealtWithTargetTypeService;
import com.jwipc.sys.service.ISapLogInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/1/30 13:04
 * @desc 指标分类
 */
@Service
@RequiredArgsConstructor
public class DealtTypeLogicImpl implements DealtTypeLogic {
    private final IDealtWithTargetTypeService dealtWithTargetTypeService;
    private final DealtTypeConvert dealtTypeConvert;
    @Override
    public PageView findByPage(DealtTypeQuery query) {
        final Page<DealtWithTargetType> dbPage = PageUtil.toPage(query);
        List<DealtWithTargetTypePO> dealtWithTargetTypePOS = dealtWithTargetTypeService.findPageByQuery(dbPage, query);
        return PageUtil.getPV(dealtTypeConvert.po2DTOList(dealtWithTargetTypePOS), dbPage.getTotal());
    }

    @Override
    public DealtWithTargetTypeDTO getById(Long id) {
        DealtWithTargetType dealtWithTargetType = dealtWithTargetTypeService.getById(id);
        return dealtTypeConvert.entity2DTO(dealtWithTargetType);
    }

    @Override
    public void saveOrUpdate(DealtWithTargetTypeDTO param) {
        //保存
        if (param.getId() == null) {
            if (ObjectUtil.isNotNull(dealtWithTargetTypeService.getOne(new LambdaQueryWrapper<DealtWithTargetType>().eq(
                    DealtWithTargetType::getCode, param.getCode()).last(" limit 1")))) {
                throw new DaoException("指标分类编码已经存在, 请检查");
            }
            param.setCreateTime(LocalDateTime.now());
            param.setCreateUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
            param.setCreateUserName(LoginUtil.getCurrentUser().getName());
        } else {
            if (ObjectUtil.isNotNull(dealtWithTargetTypeService.getOne(new LambdaQueryWrapper<DealtWithTargetType>().eq(
                    DealtWithTargetType::getCode, param.getCode()).ne(DealtWithTargetType::getId, param.getId()).last(" limit 1")))) {
                throw new DaoException("指标分类编码已经存在, 请检查");
            }
            param.setModifyTime(LocalDateTime.now());
            param.setModifyUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        }
        dealtWithTargetTypeService.saveOrUpdate(dealtTypeConvert.dto2Entity(param));
    }

    @Override
    public void remove(Long id) {
        dealtWithTargetTypeService.removeById(id);
    }

    @Override
    public List<DealtWithTargetTypeDTO> listAll(DealtTypeQuery query) {
        List<DealtWithTargetTypePO> dealtWithTargetTypePOS = dealtWithTargetTypeService.findPageByQuery(null, query);
        return dealtTypeConvert.po2DTOList(dealtWithTargetTypePOS);
    }
}
