package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.business.domain.ErpPost;
import com.maintain.business.domain.vo.ErpGroupVo;
import com.maintain.business.mapper.*;
import com.maintain.common.core.domain.entity.SysDept;
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 com.maintain.system.mapper.SysDeptMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpUserBo;
import com.maintain.business.domain.vo.ErpUserVo;
import com.maintain.business.domain.ErpUser;
import com.maintain.business.service.IErpUserService;

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

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

    private final ErpUserMapper baseMapper;
    private final ErpGroupMapper siteGroupMapper;
    private final ErpPostMapper postMapper;
    private final SysDeptMapper deptMapper;

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

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

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

    /**
     * 根据岗位ID获取员工工号
     *
     * @param postId
     * @return
     */
    @Override
    public String getUserCodeByPostId(Long postId) {
        ErpPost erpPost = postMapper.selectById(postId);
        int randomNum = (int)(Math.random() * 90000) + 10000;
        return erpPost.getCode() + randomNum;
    }

    private LambdaQueryWrapper<ErpUser> buildQueryWrapper(ErpUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getJobNumber()), ErpUser::getJobNumber, bo.getJobNumber());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpUser::getName, bo.getName());
        lqw.eq(bo.getDeptId() != null, ErpUser::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), ErpUser::getDeptName, bo.getDeptName());
        lqw.eq(bo.getSiteGroupId() != null, ErpUser::getSiteGroupId, bo.getSiteGroupId());
        lqw.like(StringUtils.isNotBlank(bo.getSiteGroupName()), ErpUser::getSiteGroupName, bo.getSiteGroupName());
        lqw.eq(bo.getState() != null, ErpUser::getState, bo.getState());
        lqw.eq(bo.getType() != null, ErpUser::getType, bo.getType());
        lqw.eq(bo.getCreateTime() != null, ErpUser::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增维修人员-人员
     */
    @Override
    public Boolean insertByBo(ErpUserBo bo) {
        ErpUser add = BeanUtil.toBean(bo, ErpUser.class);
        ErpGroupVo siteGroupVo = siteGroupMapper.selectVoById(add.getSiteGroupId());
        add.setSiteGroupName(siteGroupVo.getName());
        SysDept sysDept = deptMapper.selectVoById(siteGroupVo.getDeptId());
        add.setDeptName(sysDept.getDeptName());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改维修人员-人员
     */
    @Override
    public Boolean updateByBo(ErpUserBo bo) {
        ErpUser update = BeanUtil.toBean(bo, ErpUser.class);
        ErpGroupVo siteGroupVo = siteGroupMapper.selectVoById(update.getSiteGroupId());
        update.setSiteGroupName(siteGroupVo.getName());
        SysDept sysDept = deptMapper.selectVoById(siteGroupVo.getDeptId());
        update.setDeptName(sysDept.getDeptName());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

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