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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.PhoneUtil;
import com.bonus.common.biz.constant.MaterialConstants;
import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.clz.domain.BmTeam;
import com.bonus.material.clz.domain.vo.IwsTeamUserVo;
import com.bonus.material.clz.mapper.BmTeamMapper;
import com.bonus.material.clz.mapper.IwsTeamUserMapper;
import com.bonus.material.clz.service.BmTeamService;
import org.springframework.stereotype.Service;

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

/**
 * 班组管理接口实现类
 * @Author ma_sh
 * @create 2025/5/12 13:03
 */
@Service
public class BmTeamServiceImpl implements BmTeamService {

    @Resource
    private BmTeamMapper bmTeamMapper;

    @Resource
    private IwsTeamUserMapper iwsTeamUserMapper;

    /**
     * 查询班组信息
     * @param tbTeam
     * @return
     */
    @Override
    public List<BmTeam> queryByPage(BmTeam tbTeam) {
        List<BmTeam> list = bmTeamMapper.queryByPage(tbTeam);
        if (CollectionUtil.isNotEmpty(list)) {
            for (BmTeam bmTeam : list) {
                if (StringUtils.isNotBlank(bmTeam.getRelPhone())) {
                    if (bmTeam.getRelPhone().length() > 11) {
                        //进行解密
                        bmTeam.setRelPhone(Sm4Utils.decrypt(bmTeam.getRelPhone()));
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增班组信息
     * @param tbTeam
     * @return
     */
    @Override
    public AjaxResult insert(BmTeam tbTeam) {
        if (tbTeam == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //名称重复性校验
        BmTeam bmTeam =  bmTeamMapper.selectByName(tbTeam);
        if (bmTeam != null) {
            return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
        }
        if (getObjectResultBean(tbTeam)) {
            return AjaxResult.error(HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getCode(), HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getMsg());
        }
        tbTeam.setCreateUser(SecurityUtils.getLoginUser().getUserid().toString());
        int result = bmTeamMapper.insert(tbTeam);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    /**
     * 编辑班组信息
     * @param tbTeam
     * @return
     */
    @Override
    public AjaxResult edit(BmTeam tbTeam) {
        if (tbTeam == null || tbTeam.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        //名称重复性校验
        BmTeam bmTeam = bmTeamMapper.selectByName(tbTeam);
        if (bmTeam != null) {
            if (!Objects.equals(bmTeam.getId(), tbTeam.getId())) {
                return AjaxResult.error(HttpCodeEnum.NAME_DUPLICATE.getCode(), HttpCodeEnum.NAME_DUPLICATE.getMsg());
            }
        }
        if (getObjectResultBean(tbTeam)) {
            return AjaxResult.error(HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getCode(), HttpCodeEnum.INVALID_PHONE_NUMBER_FORMAT.getMsg());
        }
        tbTeam.setUpdateUser(SecurityUtils.getLoginUser().getUserid().toString());
        int result = bmTeamMapper.update(tbTeam);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    /**
     * 删除班组数据
     * @param tbTeam
     * @return
     */
    @Override
    public AjaxResult deleteById(BmTeam tbTeam) {
        if (tbTeam == null || tbTeam.getId() == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        int result = bmTeamMapper.deleteById(tbTeam);
        if (result > 0) {
            return AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg(), result);
        }
        return AjaxResult.error(HttpCodeEnum.UPDATE_TO_DATABASE.getCode(), HttpCodeEnum.UPDATE_TO_DATABASE.getMsg());
    }

    @Override
    public AjaxResult getList(BmTeam tbTeam) {
        List<IwsTeamUserVo> iwsTeamUserVos = iwsTeamUserMapper.selectProjectTeamInfoByIdCard(tbTeam.getIdCard());
        if (CollectionUtil.isNotEmpty(iwsTeamUserVos)) {
            return AjaxResult.success(iwsTeamUserVos);
        }
        // 根据i8工程id查询班组信息
        List<String> ids = Collections.singletonList(tbTeam.getProjectId());
        iwsTeamUserVos = iwsTeamUserMapper.selectProjectTeamInfoByProjectIds(ids);
        if (CollectionUtil.isNotEmpty(iwsTeamUserVos)) {
            return AjaxResult.success(iwsTeamUserVos);
        }
        return AjaxResult.success(new ArrayList<>());
    }

    /**
     * 查询班组信息
     * @param tbTeam
     * @return
     */
    @Override
    public AjaxResult getTeamList(BmTeam tbTeam) {
        return AjaxResult.success(bmTeamMapper.getTeamList(tbTeam));
    }

    /**
     * 根据身份证查询班组信息
     * @param tbTeam
     * @return
     */
    @Override
    public AjaxResult getListByIdCard(BmTeam tbTeam) {
        if (StringUtils.isNotBlank(tbTeam.getIdCard())) {
            // 校验身份证号是否符合正则格式
            if (!tbTeam.getIdCard().matches(MaterialConstants.CREDENTIALS_CODE_PATTERN)) {
                tbTeam.setIdCard(null);
            }
        }
        return AjaxResult.success(bmTeamMapper.queryByPage(tbTeam));
    }

    /**
     * 手机号校验方法抽取
     * @param tbTeam
     * @return
     */
    private boolean getObjectResultBean(BmTeam tbTeam) {
        if (StringUtils.isNotBlank(tbTeam.getRelPhone())) {
            if (!PhoneUtil.isMobile(tbTeam.getRelPhone())) {
                return true;
            }
        }
        return false;
    }
}
