package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.common.enums.IsDeleteStatus;
import com.maintain.common.helper.LoginHelper;
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.ErpPostBo;
import com.maintain.business.domain.vo.ErpPostVo;
import com.maintain.business.domain.ErpPost;
import com.maintain.business.mapper.ErpPostMapper;
import com.maintain.business.service.IErpPostService;

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

/**
 * 维修人员-岗位Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-25
 */
@RequiredArgsConstructor
@Service
public class ErpPostServiceImpl implements IErpPostService {

    private final ErpPostMapper baseMapper;

    /**
     * 查询维修人员-岗位
     */
    @Override
    public ErpPostVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询维修人员-岗位列表
     */
    @Override
    public TableDataInfo<ErpPostVo> queryPageList(ErpPostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpPost> lqw = buildQueryWrapper(bo);
        Page<ErpPostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修人员-岗位列表
     */
    @Override
    public List<ErpPostVo> queryList(ErpPostBo bo) {
        LambdaQueryWrapper<ErpPost> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpPost> buildQueryWrapper(ErpPostBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpPost> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null ,ErpPost::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), ErpPost::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpPost::getName, bo.getName());
        lqw.eq(bo.getCreateTime() != null, ErpPost::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getIsDelete() != null, ErpPost::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增维修人员-岗位
     */
    @Override
    public Boolean insertByBo(ErpPostBo bo) {
        ErpPost add = BeanUtil.toBean(bo, ErpPost.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改维修人员-岗位
     */
    @Override
    public Boolean updateByBo(ErpPostBo bo) {
        ErpPost update = BeanUtil.toBean(bo, ErpPost.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpPost entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<ErpPost> lqw = Wrappers.lambdaQuery();
        lqw.eq(ErpPost::getName, entity.getName());
        lqw.eq(ErpPost::getCode, entity.getCode());
        lqw.eq(ErpPost::getIsDelete, IsDeleteStatus.NO.getCode());
        lqw.ne(entity.getId() != null, ErpPost::getId, entity.getId());
        if (baseMapper.selectCount(lqw) > 0) {
            throw new RuntimeException("岗位名称与编码不能重复");
        }
    }

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