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.BsSalesmanMapper;
import com.hfkj.entity.BsSalesman;
import com.hfkj.entity.BsSalesmanExample;
import com.hfkj.entity.SecUser;
import com.hfkj.service.BsSalesmanService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.SalesmanStatusEnum;
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;

/**
 *
 * @author hurui
 */
@Service("salesmanService")
public class BsSalesmanServiceImpl implements BsSalesmanService {

    @Resource
    private BsSalesmanMapper salesmanMapper;

    @Resource
    private SecUserService userService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void insertSalesman(BsSalesman salesman, SecUser user) {
        salesman.setCreateTime(new Date());
        salesman.setUpdateTime(new Date());
        salesman.setStatus(SalesmanStatusEnum.status1.getNumber());
        salesmanMapper.insert(salesman);

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

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

    @Override
    public void updateSalesman(BsSalesman salesman) {
        salesman.setUpdateTime(new Date());
        salesmanMapper.updateByPrimaryKey(salesman);
    }

    @Override
    public void delSalesman(Long salesmanId) {
        BsSalesman salesman = getSalesman(salesmanId);
        if (salesman == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商");
        }
        if (salesman.getStatus().equals(SalesmanStatusEnum.status0)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "代理商状态错误");
        }
        salesman.setStatus(SalesmanStatusEnum.status0.getNumber());
        salesman.setUpdateTime(new Date());
        salesmanMapper.updateByPrimaryKey(salesman);

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

    @Override
    public BsSalesman getSalesman(Long salesmanId) {
        return salesmanMapper.selectByPrimaryKey(salesmanId);
    }

    @Override
    public List<BsSalesman> getSalesmanList(Map<String, Object> param) {
        BsSalesmanExample example = new BsSalesmanExample();
        BsSalesmanExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(SalesmanStatusEnum.status0.getNumber());

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

        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, "phone"))) {
            criteria.andPhoneLike("%"+MapUtils.getString(param, "phone")+"%");
        }

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

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