package cn.com.countrygarden.bi.main.service.manager.map.impl;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.manager.map.FenghuangHouseKeeperCommunicationMapper;
import cn.com.countrygarden.bi.main.dao.manager.map.ManagerHousekeeperFollowMapper;
import cn.com.countrygarden.bi.main.dao.manager.map.ManagerMapMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.ManagerMapForErpMapper;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprCommunityMapper;
import cn.com.countrygarden.bi.main.dto.HouseKeeperDTO;
import cn.com.countrygarden.bi.main.dto.HouseKeeperNameTreeDTO;
import cn.com.countrygarden.bi.main.dto.HouseKeeperParamVo;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.manager.map.ManagerHousekeeperFollow;
import cn.com.countrygarden.bi.main.service.UserRoleService;
import cn.com.countrygarden.bi.main.service.manager.map.ManagerHousekeeperFollowService;
import com.github.pagehelper.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
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 www.yuhi.com.cn
 * @date 2018-06-29
 */
@Service
@Transactional
public class ManagerHousekeeperFollowServiceImpl implements ManagerHousekeeperFollowService{
    @Autowired
    private ManagerHousekeeperFollowMapper managerHousekeeperFollowMapper;
    @Autowired
	private ManagerMapForErpMapper managerMapForErpMapper;
	@Autowired
	private FenghuangHouseKeeperCommunicationMapper houseKeeperCommunicationMapper;
	@Resource
	private TbHsprCommunityMapper tbHsprCommunityMapper;
	@Autowired
	private ManagerMapMapper managerMapMapper;
	@Autowired
	private UserRoleService userRoleService;
	
    @Override
    public Result queryPageList(HouseKeeperParamVo houseKeeperParamVo, int page, int size, AccessToken accessToken) {
		List<HouseKeeperDTO> housekeeperCaches = new ArrayList<>();
		double ceil = 0 ;
		houseKeeperParamVo.setUserCode(accessToken.getUserCode());
		Integer count = userRoleService.countByUserCodeAndRoleCode(accessToken.getUserCode(), "manager_map_admin");
		if(count>0) {//总部
			housekeeperCaches  = managerHousekeeperFollowMapper.selectByQuery(houseKeeperParamVo,null,(page-1)*size,size);
			ceil = (double) managerHousekeeperFollowMapper.countPageList(houseKeeperParamVo,null) / size;
		}else{
			List<String> regions = managerMapForErpMapper.queryRegionByUserCode(accessToken.getUserCode());
			housekeeperCaches = managerHousekeeperFollowMapper.selectByQuery(houseKeeperParamVo,regions,(page-1)*size,size);
			ceil = (double) managerHousekeeperFollowMapper.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 Result queryById(Integer id) {
        return Response.response(managerHousekeeperFollowMapper.selectByPrimaryKey(id));
    }

    @Override
    public Result add(ManagerHousekeeperFollow managerHousekeeperFollow, AccessToken accessToken) {
        managerHousekeeperFollowMapper.insert(managerHousekeeperFollow);
        return Response.response();
    }

    @Override
    public Result edit(ManagerHousekeeperFollow managerHousekeeperFollow, AccessToken accessToken) {
        managerHousekeeperFollowMapper.updateByPrimaryKey(managerHousekeeperFollow);
        return Response.response();
    }

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

    @Override
    public Result addFollow(Integer bid, String hjid, String organName, String post, AccessToken accessToken) {
        ManagerHousekeeperFollow follow=new ManagerHousekeeperFollow();
        follow.setPost(post);
        follow.setBid(bid);
        follow.setHjid(hjid);
        follow.setRegion(organName);
        follow.setCreatedBy(accessToken.getUserCode());
        follow.setCreatedDate(new Date());
        follow.setUserCode(accessToken.getUserCode());
        managerHousekeeperFollowMapper.insert(follow);
        return Response.response();
    }

    @Override
    public Result deleteFollow(Integer bid,Integer hjid, AccessToken accessToken) {
        Condition condition=new Condition(ManagerHousekeeperFollow.class);
        condition.createCriteria().andEqualTo("hjid",hjid).andEqualTo("bid",bid).andEqualTo("userCode",accessToken.getUserCode());
        managerHousekeeperFollowMapper.deleteByCondition(condition);
        return Response.response();
    }
    
	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;
	}
	
	@Override
	public Result getHouseKeeperFollowNameTree(AccessToken accessToken) {
		List<HouseKeeperNameTreeDTO> dto=managerHousekeeperFollowMapper.selectHouseKeeperFollowName(accessToken.getUserCode());
		for(HouseKeeperNameTreeDTO nameTreeDTO : dto) {
			List<HouseKeeperNameTreeDTO> pdto=managerHousekeeperFollowMapper.findPhoneNumTreeByBid(nameTreeDTO.getCode());
			nameTreeDTO.setChildren(pdto);
		}
		return Response.response(dto);
	}

	@Override
	public Result getGardenDistrictTree(AccessToken accessToken) {
		return Response.response(managerHousekeeperFollowMapper.selectFollowGardenDistrictTree(accessToken.getUserCode()));
	}


}