package cn.com.countrygarden.bi.main.service.advise.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.advise.AfAdviseBranchTypeMapper;
import cn.com.countrygarden.bi.main.dao.sys.RoleMapper;
import cn.com.countrygarden.bi.main.dao.sys.UserRolesMapper;
import cn.com.countrygarden.bi.main.dto.advise.param.BranchTypeParam;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseBranchType;
import cn.com.countrygarden.bi.main.model.sys.Role;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseBranchTypeService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 建言分支类型-业务逻辑层实现
 *
 * @author 木木子
 * @email 944528607@qq.com
 * @url
 * @date 2019-04-15
 */
@Service
public class AfAdviseBranchTypeServiceImpl implements IAfAdviseBranchTypeService {
    @Autowired
    private AfAdviseBranchTypeMapper afAdviseBranchTypeMapper;
    @Autowired
    private UserRolesMapper userRolesMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result queryList(AfAdviseBranchType afAdviseBranchType) {

        Condition condition = new Condition(AfAdviseBranchType.class);

        Criteria criteria = condition.createCriteria();
        condition.selectProperties("id,code,roleCode,name,isDefault");
        criteria.andEqualTo("enable",1);

        List<AfAdviseBranchType> afAdviseBranchTypes  = afAdviseBranchTypeMapper.selectByCondition(condition);

        return Response.response(afAdviseBranchTypes);
    }

    @Override
    public List<AfAdviseBranchType> getUserAdviseBranchRoleCode(BranchTypeParam branchTypeParam, String userCode) {
        //找出此登录用户所拥有的全部角色

        Condition condition = new Condition(UserRoles.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userCode", userCode);
        if (null!=branchTypeParam&&StringUtil.isNotBlank(branchTypeParam.getCode())) {
            criteria.andEqualTo("roleCode", branchTypeParam.getCode());
        }
        List<UserRoles> userRoles = userRolesMapper.selectByCondition(condition);
        if (null == userRoles||userRoles.isEmpty()) {
            return null;
        }
        List<String> roleCodes = userRoles.stream().map(u -> u.getRoleCode()).collect(Collectors.toList());
        //找出此登录用户在建言分支类型所拥有的数据

        Condition condition2 = new Condition(AfAdviseBranchType.class);
        condition2.selectProperties("id", "code", "roleCode");
      Criteria criteria2 = condition2.createCriteria();
        criteria2.andEqualTo("enable", 1);
        criteria2.andIn("code", roleCodes);
        List<AfAdviseBranchType> adviseBranchTypes = afAdviseBranchTypeMapper.selectByCondition(condition2);
        if (null != adviseBranchTypes && !adviseBranchTypes.isEmpty()) {
            return adviseBranchTypes;
        } else {
            return null;
        }

    }


    @Override
    public Result queryById(Integer id) {
        return Response.response(afAdviseBranchTypeMapper.selectByPrimaryKey(id));
    }


    @Override
    public Result del(Integer id) {
        int count = afAdviseBranchTypeMapper.deleteByPrimaryKey(id);
        return Response.response(count);
    }

    @Override
    public Result addOrEdit(AfAdviseBranchType afAdviseBranchType, AccessToken accessToken) {
        int count = 0;
        //1.判断dup系统是否有此角色，如无则不能添加，需先在dup添加此角色再操作
        Condition conditionRole = new Condition(Role.class);
        Criteria criteriaRole = conditionRole.createCriteria();
        criteriaRole.andEqualTo("code", afAdviseBranchType.getCode());

        count = roleMapper.selectCountByCondition(conditionRole);

        if (count <1) {
            return Response.response("noRole");
        }
        Condition conditionBranchType = new Condition(AfAdviseBranchType.class);
        Criteria criteriaBranchType = conditionBranchType.createCriteria();
        criteriaBranchType.andEqualTo("enable", 1);
        criteriaBranchType.andEqualTo("code", afAdviseBranchType.getCode());
        afAdviseBranchType.setEnable(1);
        afAdviseBranchType.setRoleCode(StringUtil.isBlank(afAdviseBranchType.getRoleCode()) ? afAdviseBranchType.getCode() : afAdviseBranchType.getRoleCode());
        //2.判断是
        if (afAdviseBranchType.getId() != null && afAdviseBranchType.getId() > 0) {
            criteriaBranchType.andNotEqualTo("id", afAdviseBranchType.getId());
            count = afAdviseBranchTypeMapper.selectCountByCondition(conditionBranchType);
            if (count > 0) {
                return Response.response("exist");
            }
            afAdviseBranchType.setUpdateBy(accessToken.getUserCode());
            afAdviseBranchType.setUpdateTime(new Date());
            count = afAdviseBranchTypeMapper.updateByPrimaryKeySelective(afAdviseBranchType);

        } else {
            count = afAdviseBranchTypeMapper.selectCountByCondition(conditionBranchType);
            if (count > 0) {
                return Response.response("exist");
            }
            afAdviseBranchType.setCreateBy(accessToken.getUserCode());
            afAdviseBranchType.setCreateTime(new Date());
            count = afAdviseBranchTypeMapper.insertSelective(afAdviseBranchType);
        }
        return Response.response(count);
    }




}