package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.ErpProjectType;
import com.maintain.business.mapper.ErpProjectTypeMapper;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpMalfunctionProjectBo;
import com.maintain.business.domain.vo.ErpMalfunctionProjectVo;
import com.maintain.business.domain.ErpMalfunctionProject;
import com.maintain.business.mapper.ErpMalfunctionProjectMapper;
import com.maintain.business.service.IErpMalfunctionProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 故障项目Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpMalfunctionProjectServiceImpl implements IErpMalfunctionProjectService {

    private final ErpMalfunctionProjectMapper baseMapper;
    private final ErpProjectTypeMapper projectTypeMapper;

    /**
     * 查询故障项目
     */
    @Override
    public ErpMalfunctionProjectVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询故障项目列表
     */
    @Override
    public TableDataInfo<ErpMalfunctionProjectVo> queryPageList(ErpMalfunctionProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMalfunctionProject> lqw = buildQueryWrapper(bo);
        Page<ErpMalfunctionProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询故障项目列表
     */
    @Override
    public List<ErpMalfunctionProjectVo> queryList(ErpMalfunctionProjectBo bo) {
        LambdaQueryWrapper<ErpMalfunctionProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMalfunctionProject> buildQueryWrapper(ErpMalfunctionProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMalfunctionProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeId() != null, ErpMalfunctionProject::getTypeId, bo.getTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), ErpMalfunctionProject::getTypeName, bo.getTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getMalfunctionCode()), ErpMalfunctionProject::getMalfunctionCode, bo.getMalfunctionCode());
        lqw.like(StringUtils.isNotBlank(bo.getMalfunctionName()), ErpMalfunctionProject::getMalfunctionName, bo.getMalfunctionName());
        lqw.eq(bo.getState() != null, ErpMalfunctionProject::getState, bo.getState());
        lqw.eq(bo.getIsDelete() != null, ErpMalfunctionProject::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增故障项目
     */
    @Override
    public Boolean insertByBo(ErpMalfunctionProjectBo bo) {
        ErpMalfunctionProject add = BeanUtil.toBean(bo, ErpMalfunctionProject.class);
        validEntityBeforeSave(add);
        int randomNum = (int)(Math.random() * 90000) + 10000;
        add.setMalfunctionCode("GZ" + randomNum);
        ErpProjectType projectType = projectTypeMapper.selectById(add.getTypeId());
        add.setTypeName(projectType.getTypeName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改故障项目
     */
    @Override
    public Boolean updateByBo(ErpMalfunctionProjectBo bo) {
        ErpMalfunctionProject update = BeanUtil.toBean(bo, ErpMalfunctionProject.class);
        validEntityBeforeSave(update);
        ErpProjectType projectType = projectTypeMapper.selectById(update.getTypeId());
        update.setTypeName(projectType.getTypeName());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpMalfunctionProject entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<ErpMalfunctionProject> projectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        projectLambdaQueryWrapper.ne(entity.getId() != null, ErpMalfunctionProject::getId, entity.getId());
        projectLambdaQueryWrapper.eq(ErpMalfunctionProject::getMalfunctionCode, entity.getMalfunctionCode());
        Long count = baseMapper.selectCount(projectLambdaQueryWrapper);
        if (count > 0) {
            throw new RuntimeException("故障编号已存在，请调整后重试");
        }
    }

    /**
     * 批量删除故障项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
