package com.hfkj.service.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsAgentMapper;
import com.hfkj.entity.BsAgent;
import com.hfkj.entity.BsAgentExample;
import com.hfkj.entity.SecUser;
import com.hfkj.service.BsAgentService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.AgentStatusEnum;
import com.hfkj.sysenum.SecUserRoleIdEnum;
import com.hfkj.sysenum.SecUserTypeEnum;
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;

@Service("agentService")
public class BsAgentServiceImpl implements BsAgentService {

    @Resource
    private BsAgentMapper agentMapper;

    @Resource
    private SecUserService userService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void insertAgent(BsAgent agent, SecUser user) {
        agent.setCreateTime(new Date());
        agent.setUpdateTime(new Date());
        agent.setStatus(AgentStatusEnum.status1.getNumber());
        agentMapper.insert(agent);

        if (userService.findByLoginName(user.getLoginName()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "账户已存在，请更换");
        }

        user.setUserName(agent.getName());
        user.setAdminFlag(1);
        user.setRoleId(SecUserRoleIdEnum.roleId3.getRoleId());
        user.setObjectType(SecUserTypeEnum.type2.getNumber());
        user.setObjectId(agent.getId());
        user.setStatus(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userService.addUser(user);
    }

    @Override
    public void updateAgent(BsAgent agent) {
        agent.setUpdateTime(new Date());
        agentMapper.updateByPrimaryKey(agent);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void delAgent(Long agentId) {
        BsAgent agent = getAgent(agentId);
        if (agent == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商");
        }
        if (agent.getStatus().equals(AgentStatusEnum.status0)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "代理商状态错误");
        }
        agent.setStatus(AgentStatusEnum.status0.getNumber());
        agent.setUpdateTime(new Date());
        agentMapper.updateByPrimaryKey(agent);

        // 查询代理商账户
        SecUser user = userService.getMainAccount(SecUserTypeEnum.type2.getNumber(), agentId);
        if (user != null) {
            user.setStatus(0);
            user.setUpdateTime(new Date());
            userService.updateUser(user);
        }
    }

    @Override
    public BsAgent getAgent(Long agentId) {
        return agentMapper.selectByPrimaryKey(agentId);
    }

    @Override
    public List<BsAgent> listAgent(Map<String, Object> param) {
        BsAgentExample example = new BsAgentExample();
        BsAgentExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(AgentStatusEnum.status0.getNumber());

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

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

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

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

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

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