package com.hfkj.service.agent.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.dao.BsAgentStaffMapper;
import com.hfkj.entity.BsAgentStaff;
import com.hfkj.entity.BsAgentStaffExample;
import com.hfkj.entity.SecUser;
import com.hfkj.service.agent.BsAgentStaffService;
import com.hfkj.service.sec.SecUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.SecUserStatusEnum;
import com.hfkj.sysenum.agent.AgentStaffStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: BsAgentStaffServiceImpl
 * @author: HuRui
 * @date: 2024/6/17
 **/
@Service("agentStaffService")
public class BsAgentStaffServiceImpl implements BsAgentStaffService {
    @Resource
    private BsAgentStaffMapper agentStaffMapper;

    @Resource
    private SecUserService userService;
    @Override
    public void editData(BsAgentStaff agentStaff) {
        if (agentStaff.getId() == null) {
            agentStaff.setCreateTime(new Date());
            agentStaff.setUpdateTime(new Date());
            agentStaffMapper.insert(agentStaff);
        } else {
            agentStaff.setUpdateTime(new Date());
            agentStaffMapper.updateByPrimaryKey(agentStaff);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void addStaff(BsAgentStaff agentStaff) throws Exception {
        if (userService.getDetailByLoginName(agentStaff.getTelephone()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "电话已存在，请更换");
        }
        agentStaff.setStatus(AgentStaffStatusEnum.status1.getStatus());
        editData(agentStaff);

        SecUser secUser = new SecUser();
        secUser.setAvatar(agentStaff.getAvatar());
        secUser.setUserName(agentStaff.getName());
        secUser.setLoginName(agentStaff.getTelephone());
        secUser.setPassword(MD5Util.encode("123654".getBytes()));
        secUser.setStatus(1);
        secUser.setRoleId(6L);
        secUser.setObjectType(SecUserObjectTypeEnum.type5.getCode());
        secUser.setObjectId(agentStaff.getId());
        secUser.setCreateTime(new Date());
        secUser.setUpdateTime(new Date());
        userService.editUser(secUser);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void updateStaff(BsAgentStaff gasStaff) {
        // 查询账户
        SecUser account = userService.getDetail(SecUserObjectTypeEnum.type5, gasStaff.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户信息");
        }
        if (!account.getLoginName().equals(gasStaff.getTelephone())) {
            if (userService.getDetailByLoginName(gasStaff.getTelephone()) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已存在，请更换");
            }
            account.setLoginName(gasStaff.getTelephone());
        }
        editData(gasStaff);

        account.setUserName(gasStaff.getName());
        userService.editUser(account);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void delStaff(Long id) {
        BsAgentStaff agentStaff = getStaffDetailById(id);
        if (agentStaff ==  null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到工作人员");
        }
        agentStaff.setStatus(AgentStaffStatusEnum.status0.getStatus());
        editData(agentStaff);

        // 查询账户
        SecUser account = userService.getDetail(SecUserObjectTypeEnum.type5, agentStaff.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
        }
        account.setStatus(SecUserStatusEnum.status0.getCode());
        userService.editUser(account);
    }

    @Override
    public void staffPwdReset(Long id) {
        try {
            BsAgentStaff staff = getStaffDetailById(id);
            if (staff == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到工作人员信息");
            }
            // 查询账户
            SecUser account = userService.getDetail(SecUserObjectTypeEnum.type5, staff.getId());
            if (account == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到工作人员账户信息");
            }
            account.setPassword(MD5Util.encode("123654".getBytes()));
            userService.editUser(account);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public BsAgentStaff getStaffDetailById(Long id) {
        return agentStaffMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsAgentStaff> getStaffList(Map<String, Object> param) {
        BsAgentStaffExample example = new BsAgentStaffExample();
        BsAgentStaffExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getLong(param, "agentId") != null) {
            criteria.andAgentIdEqualTo(MapUtils.getLong(param, "agentId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "name"))) {
            criteria.andNameLike("%" + MapUtils.getString(param, "name") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "telephone"))) {
            criteria.andTelephoneLike("%" + MapUtils.getString(param, "telephone") + "%");
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return agentStaffMapper.selectByExample(example);
    }

    @Override
    public List<BsAgentStaff> getStaffList(Long agentId) {
        BsAgentStaffExample example = new BsAgentStaffExample();
        example.createCriteria().andAgentIdEqualTo(agentId).andStatusNotEqualTo(0);
        return agentStaffMapper.selectByExample(example);
    }
}
