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.AfAdviseResponsiblerMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbSysOrganMapper;
import cn.com.countrygarden.bi.main.dto.PageDataVo;
import cn.com.countrygarden.bi.main.dto.advise.AdviseResponsiblerDTO;
import cn.com.countrygarden.bi.main.dto.advise.RegionListVO;
import cn.com.countrygarden.bi.main.dto.advise.param.AdviseFeedbackParam;
import cn.com.countrygarden.bi.main.dto.advise.param.AdviseResponsiblerParam;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseResponsibler;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseResponsiblerService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import java.util.ArrayList;
import java.util.Arrays;
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 AfAdviseResponsiblerServiceImpl implements IAfAdviseResponsiblerService{
    @Autowired
    private AfAdviseResponsiblerMapper afAdviseResponsiblerMapper;

    @Autowired
    private TbSysOrganMapper tbSysOrganMapper;


    @Override
    public Result queryList(AdviseResponsiblerParam adviseResponsiblerParam, AccessToken accessToken) {
        List<AfAdviseResponsibler> list=null;
        if(StringUtils.isNotBlank(adviseResponsiblerParam.getBranchTypeCode())) {
            Page<Object> pageList = PageHelper.startPage(adviseResponsiblerParam.getPage(), adviseResponsiblerParam.getSize());
            AfAdviseResponsibler afAdviseResponsibler = new AfAdviseResponsibler();
            afAdviseResponsibler.setBranchTypeCode(adviseResponsiblerParam.getBranchTypeCode());
            afAdviseResponsibler.setType(adviseResponsiblerParam.getType());
            if(null!=adviseResponsiblerParam.getAdviseTypeId()){
                afAdviseResponsibler.setAdviseTypeId(adviseResponsiblerParam.getAdviseTypeId());
            }
            afAdviseResponsibler.setEnable(1);
            list = afAdviseResponsiblerMapper.select(afAdviseResponsibler);
            PageDataVo data = new PageDataVo(pageList);
            data.setData(pageList.getResult());
            return Response.response(data);
        }
        return Response.response();
    }

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

    @Override
    public Result addOrEdit(String data, String branchTypeCode,AccessToken accessToken) {
    	ArrayList<AfAdviseResponsibler> afAdviseResponsiblerList = JSON.parseObject(data, new TypeReference<ArrayList<AfAdviseResponsibler>>() {
		});
    	int count=0;
        AfAdviseResponsibler responsibler=null;
        for (AfAdviseResponsibler afAdviseResponsibler : afAdviseResponsiblerList) {
            responsibler=new AfAdviseResponsibler();
            responsibler.setUserCode(afAdviseResponsibler.getUserCode());
            responsibler.setType(afAdviseResponsibler.getType());
            responsibler.setBranchTypeCode(branchTypeCode);
            if(null!=afAdviseResponsibler.getAdviseTypeId()){
                responsibler.setAdviseTypeId(afAdviseResponsibler.getAdviseTypeId());
            }

        	AfAdviseResponsibler afAdviseResponsiblerOld=afAdviseResponsiblerMapper.selectOne(responsibler);
        	if(afAdviseResponsiblerOld==null) {
                afAdviseResponsibler.setEnable(1);
                afAdviseResponsibler.setBranchTypeCode(branchTypeCode);
        		afAdviseResponsibler.setCreateBy(accessToken.getUserCode());
				afAdviseResponsibler.setCreateTime(new Date());
				count+=afAdviseResponsiblerMapper.insertSelective(afAdviseResponsibler);
        	}else {
                afAdviseResponsiblerOld.setEnable(1);
                afAdviseResponsiblerOld.setBranchTypeCode(branchTypeCode);
                afAdviseResponsiblerOld.setCreateBy(accessToken.getUserCode());
                afAdviseResponsiblerOld.setCreateTime(new Date());
        		afAdviseResponsiblerOld.setUpdateBy(accessToken.getUserCode());
        		afAdviseResponsiblerOld.setUpdateTime(new Date());
				count+=afAdviseResponsiblerMapper.updateByPrimaryKeySelective(afAdviseResponsiblerOld);
        	}
		
		}
        return Response.response(count);
    	
    }


    @Override
    public Result del(String ids) {
      String[] idArray  =ids.split(",");
      if(idArray.length<1){
          return Response.response() ;
      }
        Condition condition=new Condition(AfAdviseResponsibler.class);
      condition.createCriteria().andIn("id", Arrays.asList(idArray));
        AfAdviseResponsibler afAdviseResponsibler=new AfAdviseResponsibler();
        afAdviseResponsibler.setEnable(0);
        int count = afAdviseResponsiblerMapper.updateByConditionSelective(afAdviseResponsibler,condition);
        return Response.response(count);
    }
    @Override
    public AdviseFeedbackParam getAdviseResponsiblerDTO(AdviseFeedbackParam adviseFeedbackParam){

      //获得此用户某分支拥有的所有角色

        List<AdviseResponsiblerDTO> responsiblerDTOList= afAdviseResponsiblerMapper.selectAdviseResponsiblerDTOAll(adviseFeedbackParam.getUserCode(),adviseFeedbackParam.getBranchTypeCode());
        if(null!=responsiblerDTOList&&!responsiblerDTOList.isEmpty()) {
            adviseFeedbackParam.setRole(true);


            //是否超级管理员，超级管理员有查看私密建言的权限
            long count = responsiblerDTOList.stream().filter(item -> item.getType().equals(0)).count();
            if (count > 0) {
                adviseFeedbackParam.setRole(true);
                adviseFeedbackParam.setSuperAdminRole(true);
            }
            //是否总部管理员
            count = responsiblerDTOList.stream().filter(item -> item.getType().equals(1)).count();
            if (count > 0) {
                adviseFeedbackParam.setRole(true);
                adviseFeedbackParam.setHeadquartersAdminRole(true);
            }

            //是否类型管理员
            List<AdviseResponsiblerDTO> collect = responsiblerDTOList.stream().filter(item -> item.getType().equals(2)).collect(Collectors.toList());

            if (null!=collect&&!collect.isEmpty()) {
                adviseFeedbackParam.setRole(true);
                adviseFeedbackParam.setTypeAdminRole(true);
                List<Integer> adviseTypes = collect.stream().map(u -> u.getAdviseTypeId()).collect(Collectors.toList());
                if(null!=adviseTypes){
                    adviseFeedbackParam.setTypeAdminAdviseTypeId(adviseTypes);
                }
            }

            //是否区域管理员
           count = responsiblerDTOList.stream().filter(item -> item.getType().equals(3)).count();

            if (count>0) {
                adviseFeedbackParam.setRole(true);
                //如是区域管理员，则同时获得当前登录人的所属区域
                adviseFeedbackParam.setRegionAdminRole(true);
                    List<RegionListVO> regionList = tbSysOrganMapper.findArea(null,adviseFeedbackParam.getUserCode());
                    if(null!=regionList&&!regionList.isEmpty()){
                        adviseFeedbackParam.setRegionList(regionList);
                    }
            }
        }
        return adviseFeedbackParam;
    }

}