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.ManagerHousekeeperCacheMapper;
import cn.com.countrygarden.bi.main.dao.manager.map.ManagerHousekeeperFollowMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxRegionProjectRelMapper;
import cn.com.countrygarden.bi.main.dto.HouseKeeperDTO;
import cn.com.countrygarden.bi.main.dto.HouseKeeperParamVo;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.manager.map.ManagerHousekeeperCache;
import cn.com.countrygarden.bi.main.service.UserRoleService;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerHousekeeperCacheService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
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.lang3.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 管家列表数据缓存-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-11-19
 */
@Service
public class ManagerHousekeeperCacheServiceImpl implements ManagerHousekeeperCacheService {
    @Autowired
    private ManagerHousekeeperCacheMapper managerHousekeeperCacheMapper;
    @Autowired
    private ManagerHousekeeperFollowMapper managerHousekeeperFollowMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private WcxRegionProjectRelMapper wcxRegionProjectRelMapper;
    @Autowired
    private ManagerMapForErpMapper managerMapForErpMapper;

    @Override
    public Result queryPageList(ManagerHousekeeperCache managerHousekeeperCache, int page, int size) {
        Page<Object> pageList = PageHelper.startPage(page, size);
        Condition condition=new Condition(ManagerHousekeeperCache.class);
        Example.Criteria criteria = condition.createCriteria();
//		TODO 添加条件
		managerHousekeeperCacheMapper.selectByCondition(condition);
        PageData data=new PageData(pageList);
        data.setData(pageList.getResult());
        return Response.response(data);
    }

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

    @Override
    public Result add(ManagerHousekeeperCache managerHousekeeperCache, AccessToken accessToken) {
        managerHousekeeperCacheMapper.insert(managerHousekeeperCache);
        return Response.response();
    }

    @Override
    public Result edit(ManagerHousekeeperCache managerHousekeeperCache, AccessToken accessToken) {
        managerHousekeeperCacheMapper.updateByPrimaryKey(managerHousekeeperCache);
        return Response.response();
    }

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

    @Override
    public Result queryPageList(HouseKeeperParamVo houseKeeperParamVo, Integer page, Integer size, AccessToken accessToken) {
        List<HouseKeeperDTO> housekeeperCaches = new ArrayList<>();
        double ceil = 0 ;
        Integer count = userRoleService.countByUserCodeAndRoleCode(accessToken.getUserCode(), "manager_map_admin");
        if(count>0) {//总部
            housekeeperCaches =  managerHousekeeperCacheMapper.selectByQuery(houseKeeperParamVo,null,(page-1)*size,size);
            ceil = (double) managerHousekeeperCacheMapper.countPageList(houseKeeperParamVo,null) / size;
        }else{
            List<String> regions = managerMapForErpMapper.queryRegionByUserCode(accessToken.getUserCode());
            housekeeperCaches =  managerHousekeeperCacheMapper.selectByQuery(houseKeeperParamVo,regions,(page-1)*size,size);
            ceil = (double) managerHousekeeperCacheMapper.countPageList(houseKeeperParamVo,regions) / size;
        }
        PageData data=new PageData(new Page(page,size));
        if(CollectionUtils.isNotEmpty(housekeeperCaches)) {
            for(HouseKeeperDTO dto : housekeeperCaches){
                if(dto.getJurisdictionMonth()!=null){
                    dto.setJurisdictionForMonth(String.valueOf(countMonths(dto.getJurisdictionMonth())));
                }
                HouseKeeperDTO one = managerHousekeeperFollowMapper.selectHouseKeeperFllowById(dto.getBid(),dto.getHjid(),houseKeeperParamVo.getUserCode());
                if(one!=null){
                    dto.setFollowFlag(1);
                }
            }
        }
        data.setData(housekeeperCaches);

        if(ceil>(int)ceil){
            ceil = (int) ceil + 1;
        }
        data.setPages((int) ceil);
        return Response.response(data);
    }

    @Override
    public List<HouseKeeperDTO> queryDataList(HouseKeeperParamVo houseKeeperParamVo, AccessToken accessToken) {
//        List<HouseKeeperDTO> housekeeperCaches = managerHousekeeperCacheMapper.selectByQuery(houseKeeperParamVo,null,null);
//        权限控制
        Integer count = userRoleService.countByUserCodeAndRoleCode(accessToken.getUserCode(), "manager_map_admin");
        List<HouseKeeperDTO> housekeeperCaches = new ArrayList<>();
        if(count>0) {//总部
            housekeeperCaches = managerHousekeeperCacheMapper.selectByQuery(houseKeeperParamVo,null,null,null);
        }else{
            List<String> regionName = managerMapForErpMapper.queryRegionByUserCode(accessToken.getUserCode());
            houseKeeperParamVo.setJobLv("管家");
            housekeeperCaches= managerHousekeeperCacheMapper.selectByQueryByUser(houseKeeperParamVo,regionName,null,null);
        }
        if(CollectionUtils.isNotEmpty(housekeeperCaches)) {
            for(HouseKeeperDTO dto : housekeeperCaches){
                if(dto.getJurisdictionMonth()!=null){
                    dto.setJurisdictionForMonth(String.valueOf(countMonths(dto.getJurisdictionMonth())));
                }
                HouseKeeperDTO one = managerHousekeeperFollowMapper.selectHouseKeeperFllowById(dto.getBID(),dto.getHjid(),houseKeeperParamVo.getUserCode());
                if(one!=null){
                    dto.setFollowFlag(1);
                }
            }
        }
        return housekeeperCaches;
    }

    @Override
    public Integer editData(ManagerHousekeeperCache managerHousekeeperCache) {
        managerHousekeeperCache.setIsUpdate(1);
        Condition condition = new Condition(ManagerHousekeeperCache.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("regionName",managerHousekeeperCache.getRegionName());
        criteria.andEqualTo("pkOrg",managerHousekeeperCache.getPkOrg());
        criteria.andEqualTo("name",managerHousekeeperCache.getName());
        return managerHousekeeperCacheMapper.updateByConditionSelective(managerHousekeeperCache,condition);
    }


    private Integer countMonths(Date JurisdictionMonth) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(date);
        aft.setTime(JurisdictionMonth);
        int month1 = bef.get(Calendar.MONTH)-aft.get(Calendar.MONTH);
        int month2 = (bef.get(Calendar.YEAR)-aft.get(Calendar.YEAR))*12;
        int month = month1+month2;
        return month;
    }
}