package com.bonus.material.clz.service.impl;

import cn.hutool.core.util.PhoneUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.clz.domain.BmProDepart;
import com.bonus.material.clz.domain.BmTeam;
import com.bonus.material.clz.mapper.BmProDepartMapper;
import com.bonus.material.clz.mapper.BmTeamMapper;
import com.bonus.material.clz.service.BmProDepartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 项目部管理实现层
 * @Author ma_sh
 * @create 2025/5/12 10:12
 */
@Service
public class BmProDepartServiceImpl implements BmProDepartService {

    @Resource
    private BmProDepartMapper bmProDepartMapper;

    @Resource
    private BmTeamMapper bmTeamMapper;

    /**
     * 查询项目部列表信息
     * @param tbProDepart
     * @return
     */
    @Override
    public List<BmProDepart> queryByPage(BmProDepart tbProDepart) {
        return bmProDepartMapper.queryByPage(tbProDepart);
    }

    /**
     * 新增项目部
     * @param tbProDepart
     * @return
     */
    @Override
    public AjaxResult insert(BmProDepart tbProDepart) {
        if (tbProDepart == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //名称重复性校验
        BmProDepart depart = bmProDepartMapper.selectByName(tbProDepart);
        if (depart != null) {
            return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
        }
        if (getObjectResultBean(tbProDepart)) {
            return AjaxResult.error(HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getCode(), HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getMsg());
        }
        tbProDepart.setCreateUser(SecurityUtils.getLoginUser().getUserid());
        int result = bmProDepartMapper.insert(tbProDepart);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    /**
     * 编辑项目部
     * @param tbProDepart
     * @return
     */
    @Override
    public AjaxResult update(BmProDepart tbProDepart) {
        if (tbProDepart == null || tbProDepart.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //名称重复性校验
        BmProDepart depart = bmProDepartMapper.selectByName(tbProDepart);
        if (depart != null) {
            if (!Objects.equals(depart.getId(), tbProDepart.getId())) {
                return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
            }
        }
        if (getObjectResultBean(tbProDepart)) {
            return AjaxResult.error(HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getCode(), HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getMsg());
        }
        tbProDepart.setUpdateUser(SecurityUtils.getLoginUser().getUserid());
        int result = bmProDepartMapper.update(tbProDepart);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    /**
     * 删除项目部
     * @param tbProDepart
     * @return
     */
    @Override
    public AjaxResult deleteById(BmProDepart tbProDepart) {
        if (tbProDepart == null || tbProDepart.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        // 先根据项目部id查询，该项目部是否被班组关联，若被关联，则无法删除
        BmTeam bmTeam = new BmTeam();
        bmTeam.setDepartId(tbProDepart.getId());
        List<BmTeam> list = bmTeamMapper.queryByPage(bmTeam);
        if (CollectionUtils.isNotEmpty(list)) {
            return AjaxResult.error(HttpCodeEnum.DEPART_IS_RELATED_TEAM.getCode(), HttpCodeEnum.DEPART_IS_RELATED_TEAM.getMsg());
        }
        // 进行删除操作
        int result = bmProDepartMapper.deleteById(tbProDepart);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    /**
     * 校验手机号格式
     * @param tbProDepart
     * @return
     */
    private boolean getObjectResultBean(BmProDepart tbProDepart) {
        if (StringUtils.isNotBlank(tbProDepart.getHeadUserPhone())) {
            if (!PhoneUtil.isMobile(tbProDepart.getHeadUserPhone())) {
                return true;
            }
        }
        return false;
    }
}
