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.HighGasStaffMapper;
import com.hai.entity.HighGasStaff;
import com.hai.entity.HighGasStaffExample;
import com.hai.entity.SecUser;
import com.hai.enum_type.GasPositionType;
import com.hai.enum_type.GasStaffStatus;
import com.hai.service.HighGasStaffService;
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("gasStaffService")
public class HighGasStaffServiceImpl implements HighGasStaffService {

    @Resource
    private HighGasStaffMapper gasStaffMapper;

    @Resource
    private SecUserService userService;

    @Override
    public void editData(HighGasStaff gasStaff) {
        if (gasStaff.getId() == null) {
            gasStaff.setCreateTime(new Date());
            gasStaff.setUpdateTime(new Date());
            gasStaff.setStatus(GasStaffStatus.status1.getStatus());
            gasStaffMapper.insert(gasStaff);
        } else {
            gasStaff.setUpdateTime(new Date());
            gasStaffMapper.updateByPrimaryKey(gasStaff);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void addStaff(HighGasStaff gasStaff) throws Exception {
        if (userService.findByLoginName(gasStaff.getTelephone()) != null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "电话已存在，请更换");
        }

        editData(gasStaff);

        Long roleId;
        if (gasStaff.getPositionType().equals(GasPositionType.status1.getStatus())) {
            roleId = 100L;
        } else if (gasStaff.getPositionType().equals(GasPositionType.status2.getStatus())) {
            roleId = 101L;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知角色");
        }

        SecUser secUser = new SecUser();
        secUser.setAvatar(gasStaff.getAvatar());
        secUser.setUserName(gasStaff.getName());
        secUser.setLoginName(gasStaff.getTelephone());
        secUser.setPassword(MD5Util.encode("123456".getBytes()));
        secUser.setAdminFlag(1);
        secUser.setStatus(1);
        secUser.setRoleId(roleId);
        secUser.setObjectType(8);
        secUser.setObjectId(gasStaff.getId());
        secUser.setCreateTime(new Date());
        secUser.setUpdateTime(new Date());
        userService.addUser(secUser);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void updateStaff(HighGasStaff gasStaff) {
        HighGasStaff detail = getStaffDetailById(gasStaff.getId());
        if (detail == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
        }

        if (!detail.getTelephone().equals(gasStaff.getTelephone())) {
            if (userService.findByLoginName(gasStaff.getTelephone()) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "电话已存在，请更换");
            }
        }

        editData(gasStaff);

        // 查询账户
        SecUser account = userService.getMainAccount(8, gasStaff.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
        }
        account.setAvatar(gasStaff.getAvatar());
        account.setUserName(gasStaff.getName());
        account.setLoginName(gasStaff.getTelephone());
        account.setUpdateTime(new Date());
        userService.updateUser(account);
    }

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

        // 查询账户
        SecUser account = userService.getMainAccount(8, gasStaff.getId());
        if (account == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到账户");
        }
        account.setStatus(0);
        userService.updateUser(account);
    }

    @Override
    public HighGasStaff getStaffDetailById(Long id) {
        return gasStaffMapper.selectByPrimaryKey(id);
    }

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

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

        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, "positionType") != null) {
            criteria.andPositionTypeEqualTo(MapUtils.getInteger(param, "positionType"));
        }

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

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