package com.hai.service.impl;

import com.hai.common.utils.MD5Util;
import com.hai.dao.HighAgentMapper;
import com.hai.entity.HighAgent;
import com.hai.entity.HighAgentExample;
import com.hai.entity.SecUser;
import com.hai.model.HighAgentModel;
import com.hai.service.HighAgentService;
import com.hai.service.SecUserService;
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;

/**
 * @Auther: 袁野
 * @Description:
 * @Date: 2021/3/14 20:47
 */
@Service("highAgentService")
public class HighAgentServiceImpl implements HighAgentService {

    @Resource
    private HighAgentMapper highAgentMapper;

    @Resource
    private SecUserService secUserService;

    @Override
    public List<HighAgent> getListAgentMsg(Map<String, Object> map) throws Exception {

        HighAgentExample example = new HighAgentExample();
        HighAgentExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "parentAgentId") != null) {
            criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentAgentId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "agentName"))) {
            criteria.andAgentNameLike("%" + map.get("agentName") + "%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "agentPhone"))) {
            criteria.andAgentPhoneEqualTo(MapUtils.getString(map, "agentPhone"));
        }

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

        if (MapUtils.getInteger(map, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map, "type"));
        }

        if (MapUtils.getLong(map, "operatorId") != null) {
            criteria.andOperatorIdEqualTo(MapUtils.getLong(map, "operatorId"));
        }

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

    @Override
    public HighAgent findByAgentMsgId(Long agentMsgId) {
        return highAgentMapper.selectByPrimaryKey(agentMsgId);
    }

    @Override
    public Boolean findByAgentName(String agentName) {
        HighAgentExample example = new HighAgentExample();
        HighAgentExample.Criteria criteria = example.createCriteria();

        criteria.andAgentNameEqualTo(agentName);

        return highAgentMapper.selectByExample(example).size() != 0;
    }

    @Override
    public void updateAgentMsg(HighAgentModel highAgentModel) throws Exception {
        highAgentMapper.updateByPrimaryKey(highAgentModel);
        // 查询主账号
        SecUser mainAccount = secUserService.getMainAccount(4, highAgentModel.getId());

        if (mainAccount != null) {
            if (!mainAccount.getPassword().equals(highAgentModel.getSecUser().getPassword())) {
                String userPass = MD5Util.encode(highAgentModel.getSecUser().getPassword().getBytes());
                mainAccount.setPassword(userPass);
            }
            if (highAgentModel.getStatus() == 0) {
                mainAccount.setStatus(0);
            }
            mainAccount.setTelephone(highAgentModel.getSecUser().getLoginName());
            secUserService.updateUser(mainAccount);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void insertChildAgent(HighAgent agent) throws Exception {
        highAgentMapper.insert(agent);

        SecUser secUser = new SecUser();
        secUser.setUserName(agent.getUserName());
        secUser.setLoginName(agent.getAgentPhone());
        secUser.setPassword(MD5Util.encode("123456".getBytes()));
        secUser.setAdminFlag(1);
        secUser.setStatus(1);
        secUser.setRoleId(6L);
        secUser.setObjectType(4);
        secUser.setObjectId(agent.getId());
        secUser.setCreateTime(new Date());
        secUser.setUpdateTime(new Date());
        secUserService.addUser(secUser);
    }

    @Override
    public void insertAgentMsg(HighAgentModel highAgentModel) throws Exception  {
        highAgentMapper.insert(highAgentModel);

        highAgentModel.getSecUser().setUserName(highAgentModel.getAgentName());
        highAgentModel.getSecUser().setLoginName(highAgentModel.getSecUser().getLoginName());
        highAgentModel.getSecUser().setPassword(MD5Util.encode(highAgentModel.getSecUser().getPassword().getBytes()));
        highAgentModel.getSecUser().setAdminFlag(1);
        highAgentModel.getSecUser().setStatus(1);
        highAgentModel.getSecUser().setRoleId(6L);
        highAgentModel.getSecUser().setObjectType(4);
        highAgentModel.getSecUser().setObjectId(highAgentModel.getId());
        highAgentModel.getSecUser().setCreateTime(new Date());
        highAgentModel.getSecUser().setUpdateTime(new Date());
        secUserService.addUser(highAgentModel.getSecUser());
    }

    @Override
    public void updateAgentDetail(HighAgent highAgent) {
        highAgentMapper.updateByPrimaryKey(highAgent);
    }
}
