package cn.com.countrygarden.bi.main.service.manager.map.impl;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.dao.manager.map.ManagerMapCacheMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.model.manager.map.ManagerMapCache;
import cn.com.countrygarden.bi.main.service.UserRoleService;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerMapCacheService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import org.apache.commons.collections4.CollectionUtils;
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 tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

/**
 * 管家地图数据缓存-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-11-19
 */
@Service
public class ManagerMapCacheServiceImpl implements ManagerMapCacheService {

    @Autowired
    private ManagerMapCacheMapper managerMapCacheMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ManagerMapForErpMapper managerMapForErpMapper;

    @Override
    public Result queryPageList(ManagerMapCache managerMapCache,String userCode, int page, int size) {
        Integer count = userRoleService.countByUserCodeAndRoleCode(userCode, "manager_map_admin");
        Condition condition=new Condition(ManagerMapCache.class);
        Example.Criteria criteria = condition.createCriteria();
        if(count>0){
            criteria.andLike("organName","%"+managerMapCache.getOrganName()+"%");
        }else{
            List<String> dupOrgans = managerMapForErpMapper.queryRegionByUserCode(userCode);
            criteria.andIn("organName",dupOrgans);
            if(StringUtils.isNotEmpty(managerMapCache.getOrganName())){
                criteria.andLike("organName","%"+managerMapCache.getOrganName()+"%");
            }
        }
        criteria.andGreaterThan("housekeeperCount",0);
        condition.setDistinct(true);
        condition.selectProperties("organName","organCode","commCount","managerNumCount","houserkeeperOrganization","managerNumAvg","housekeeperCount","flag");
        Page<Object> pageList = PageHelper.startPage(page, size);
        List<ManagerMapCache> managerMapCaches = managerMapCacheMapper.selectByCondition(condition);
        if(CollectionUtils.isNotEmpty(managerMapCaches)){
            for(ManagerMapCache obj : managerMapCaches){
//                取整
                if(obj.getManagerNumCount()!=null){
                    obj.setManagerNumCount(obj.getManagerNumCount().setScale(0,BigDecimal.ROUND_HALF_UP));
//                    人均管理户数=已接管户数/管家数
                    BigDecimal housekeeperNum = obj.getHousekeeperCount() !=null?new BigDecimal(obj.getHousekeeperCount()):BigDecimal.ZERO;
                    if(housekeeperNum.compareTo(BigDecimal.ZERO)==0){
                        obj.setManagerNumAvg(BigDecimal.ZERO);
                    }else {
                        obj.setManagerNumAvg(obj.getManagerNumCount().divide(housekeeperNum,0,BigDecimal.ROUND_HALF_UP));
                    }
                }else {
                    obj.setManagerNumAvg(BigDecimal.ZERO);
                }
            }
        }
        PageData data=new PageData(pageList);
        data.setData(managerMapCaches);
        return Response.response(data);
    }

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

    @Override
    public Result add(ManagerMapCache managerMapCache, AccessToken accessToken) {
        managerMapCacheMapper.insert(managerMapCache);
        return Response.response();
    }

    @Override
    public Result edit(ManagerMapCache managerMapCache, AccessToken accessToken) {
        managerMapCacheMapper.updateByPrimaryKey(managerMapCache);
        return Response.response();
    }

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

    @Override
    public boolean saveList(List<ManagerMapCache> recordList) {
        return managerMapCacheMapper.insertList(recordList)!=-1;
    }

    @Override
    public List<ManagerMapCache> queryManagerMapExcelList(ManagerMapCache managerMapCache, String userCode) {
        Integer count = userRoleService.countByUserCodeAndRoleCode(userCode, "manager_map_admin");
        Condition condition=new Condition(ManagerMapCache.class);
        Example.Criteria criteria = condition.createCriteria();
        if(count>0){
            criteria.andLike("organName","%"+managerMapCache.getOrganName()+"%");
        }else{
            List<String> dupOrgans = managerMapForErpMapper.queryRegionByUserCode(userCode);
            criteria.andIn("organName",dupOrgans);
            if(StringUtils.isNotEmpty(managerMapCache.getOrganName())){
                criteria.andLike("organName","%"+managerMapCache.getOrganName()+"%");
            }
        }
        criteria.andGreaterThan("housekeeperCount",0);
        condition.setDistinct(true);
        condition.selectProperties("organName","organCode","commCount","managerNumCount","houserkeeperOrganization","managerNumAvg","housekeeperCount","flag");
        List<ManagerMapCache> managerMapCaches = managerMapCacheMapper.selectByCondition(condition);
        if(CollectionUtils.isNotEmpty(managerMapCaches)){
            for(ManagerMapCache obj : managerMapCaches){
//                取整
                if(obj.getManagerNumCount()!=null){
                    obj.setManagerNumCount(obj.getManagerNumCount().setScale(0,BigDecimal.ROUND_HALF_UP));
//                    人均管理户数=已接管户数/管家数
                    BigDecimal housekeeperNum = obj.getHousekeeperCount() !=null?new BigDecimal(obj.getHousekeeperCount()):BigDecimal.ZERO;
                    if(housekeeperNum.compareTo(BigDecimal.ZERO)==0){
                        obj.setManagerNumAvg(BigDecimal.ZERO);
                    }else {
                        obj.setManagerNumAvg(obj.getManagerNumCount().divide(housekeeperNum,0,BigDecimal.ROUND_HALF_UP));
                    }
                }else {
                    obj.setManagerNumAvg(BigDecimal.ZERO);
                }
            }
        }
        return managerMapCaches;
    }

}