package com.goafanti.user.service.impl;

import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.PublicReleaseCountMapper;
import com.goafanti.common.dao.UserFollowMapper;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.user.bo.InputUserFollowList;
import com.goafanti.user.bo.outFollowStatisticsList;
import com.goafanti.user.bo.outProvinceFollowStatistic;
import com.goafanti.user.bo.outUserFollowList;
import com.goafanti.user.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserFollowServiceImpl extends BaseMybatisDao<UserFollowMapper>  implements UserFollowService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PublicReleaseCountMapper publicReleaseCountMapper;
    @Autowired
    private DepartmentService departmentService;


    @Override
    public Pagination<outUserFollowList> userFollowList(InputUserFollowList in) {
        Map<String,Object>map=setParam(in);

        return (Pagination<outUserFollowList>) findPage("userFollowList","userFollowCount",map,in.getPageNo(), in.getPageSize());
    }

    private Map<String,Object> setParam(InputUserFollowList in) {
        Map<String,Object>map=new HashMap<>();
        if (in.getUserName()!=null) map.put("userName", in.getUserName());
        if (in.getContactType()!=null) map.put("contactType", in.getContactType());
        if (in.getUserType()!=null) map.put("userType", in.getUserType());
        if (in.getFollowStartTime()!=null) map.put("followStartTime", in.getFollowStartTime());
        if (in.getFollowEndTime()!=null) map.put("followEndTime", in.getFollowEndTime()+" 23:59:59");
        if (in.getCreateStartTime()!=null) map.put("createStartTime", in.getCreateStartTime());
        if (in.getCreateEndTime()!=null) map.put("createEndTime", in.getCreateEndTime()+" 23:59:59");
        if (in.getGuidanceStartTime()!=null) map.put("guidanceStartTime", in.getGuidanceStartTime());
        if (in.getGuidanceEndTime()!=null) map.put("guidanceEndTime", in.getGuidanceEndTime()+" 23:59:59");
        if (in.getDeps()!=null){
            map.put("deps", departmentService.parseArray(in.getDeps()));
        }
        if (in.getShiroType()!=null){
            map.put("shiroType",in.getShiroType());
        }
        else{
            map.put("shiroType",0);
        }
        if(in.getShiroType()==0){
        map.put("aid", TokenManager.getAdminId());
        }else if(in.getShiroType()==1){
            String aid=TokenManager.getAdminId();
            List<String> aids=new ArrayList<>();
            aids=adminMapper.selectBySuperId(aid);
            aids.add(aid);
            map.put("aids",aids);
        }
        if (in.getFollowName() != null )map.put("followName",in.getFollowName());
        return map;
    }

    @Override
    public List<outUserFollowList> userFollowListExport(InputUserFollowList in) {
        Map<String,Object>map=setParam(in);
        List<outUserFollowList>list= (List<outUserFollowList>) findList("userFollowList",map,1,999999);

        return list;
    }

    @Override
    public List<outFollowStatisticsList> followStatisticsList(String depId, String date,Integer province, Integer sort) {
        if(sort==null)sort=0;
        if (date==null)return new ArrayList<>();
        return publicReleaseCountMapper.followStatisticsList(depId,date,province,sort);
    }

    @Override
    public List<outProvinceFollowStatistic> provinceFollowStatisticsList(String depId, String date, Integer province,Integer type, Integer sort) {
        if(sort==null)sort=0;
        return publicReleaseCountMapper.provinceFollowStatisticsList(depId,date,province,type,sort);
    }
}
