package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.MD5Util;
import com.hai.dao.HighTySalesmanMapper;
import com.hai.entity.HighTyAgent;
import com.hai.entity.HighTySalesman;
import com.hai.entity.HighTySalesmanExample;
import com.hai.entity.SecUser;
import com.hai.enum_type.UserObjectTypeEnum;
import com.hai.service.HighTySalesmanService;
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;

@Service("highTySalesmanService")
public class HighTySalesmanServiceImpl implements HighTySalesmanService {

    @Resource
    private HighTySalesmanMapper tySalesmanMapper;

    @Resource
    private SecUserService secUserService;

    @Override
    public void editTySalesman(HighTySalesman tySalesman) {
        if (tySalesman.getId() == null) {
            tySalesman.setCreateTime(new Date());
            tySalesman.setUpdateTime(new Date());
            tySalesman.setStatus(1);
            tySalesmanMapper.insert(tySalesman);

            // 生成key
            Long key = 2000000 + tySalesman.getId();
            tySalesman.setSalesmanKey(key.toString());
            tySalesmanMapper.updateByPrimaryKey(tySalesman);

        } else {
            tySalesman.setUpdateTime(new Date());
            tySalesmanMapper.updateByPrimaryKey(tySalesman);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void addSalesman(HighTySalesman tySalesman) throws Exception {
        if (secUserService.findByLoginName(tySalesman.getSalesmanPhone()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "在系统中此联系电话已存在，请更换");
        }
        editTySalesman(tySalesman);

        SecUser secUser = new SecUser();
        secUser.setUserName(tySalesman.getSalesmanName());
        secUser.setLoginName(tySalesman.getSalesmanPhone());
        secUser.setPassword(MD5Util.encode("123456".getBytes()));
        secUser.setAdminFlag(1);
        secUser.setStatus(1);
        secUser.setRoleId(8L);
        secUser.setObjectType(UserObjectTypeEnum.type7.getType());
        secUser.setObjectId(tySalesman.getId());
        secUser.setCreateTime(new Date());
        secUser.setUpdateTime(new Date());
        secUserService.addUser(secUser);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void updateSalesman(HighTySalesman tySalesman) {
        // 查询账户
        SecUser account = secUserService.getMainAccount(UserObjectTypeEnum.type7.getType(), tySalesman.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户信息");
        }

        if (!account.getLoginName().equals(tySalesman.getSalesmanPhone())) {
            if (secUserService.findByPhone(tySalesman.getSalesmanPhone()) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已存在，请更换");
            }
            account.setLoginName(tySalesman.getSalesmanPhone());
        }
        editTySalesman(tySalesman);

        account.setUserName(tySalesman.getSalesmanName());
        secUserService.updateUser(account);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void delSalesman(String key) {
        HighTySalesman salesman = getDetailByKey(key);
        if (salesman == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到业务员");
        }
        salesman.setStatus(0);
        editTySalesman(salesman);

        // 查询代理商账户
        SecUser account = secUserService.getMainAccount(UserObjectTypeEnum.type7.getType(), salesman.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到业务员账户信息");
        }
        account.setStatus(0);
        secUserService.updateUser(account);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void salesmanPwdReset(String key) {
        try {
            HighTySalesman salesman = getDetailByKey(key);
            if (salesman == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到业务员");
            }
            // 查询代理商账户
            SecUser account = secUserService.getMainAccount(UserObjectTypeEnum.type7.getType(), salesman.getId());
            if (account == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到业务员账户信息");
            }
            account.setPassword(MD5Util.encode("123456".getBytes()));
            secUserService.updateUser(account);

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

    @Override
    public HighTySalesman getDetailById(Long salesmanId) {
        return tySalesmanMapper.selectByPrimaryKey(salesmanId);
    }

    @Override
    public HighTySalesman getDetailByKey(String key) {
        HighTySalesmanExample example = new HighTySalesmanExample();
        example.createCriteria().andStatusNotEqualTo(0).andSalesmanKeyEqualTo(key);
        List<HighTySalesman> list = tySalesmanMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

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

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

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

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

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

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