package com.chy.borrow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chy.borrow.entity.BorrowApply;
import com.chy.borrow.mapper.BorrowApplyMapper;
import com.chy.borrow.mapper.BorrowApplyZjl0754Mapper;
import com.chy.borrow.mapper.UserMapper;
import com.chy.borrow.service.BorrowApplyService;
import com.chy.borrow.service.BorrowApplyZjl0754Service;
import com.chy.borrow.service.StatisticsService;
import com.github.pagehelper.Page;
import com.shanghaigeography.Util.DateUtils;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.util.UntrustedCertificates;

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

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    BorrowApplyService borrowApplyService;
    @Autowired
    BorrowApplyMapper borrowApplyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BorrowApplyZjl0754Service borrowApplyZjl0754Service;
    @Autowired
    BorrowApplyZjl0754Mapper borrowApplyZjl0754Mapper;

    @Override
    public String statisticsStatusByUser(String userId) {
        List<Integer> roleList = userMapper.getRoleByUserId(userId);
        Map result = new HashMap();
        Map map = new HashMap<>();
        Map map1 = new HashMap<>();
        map.put("apply_user_id",userId);
        map.put("borrow_status","1");
        map1.put("borrow_status","1");
        int status_1 = borrowApplyService.listByMap(map).size();
        int status_1_noId = borrowApplyService.listByMap(map1).size();
        map.put("borrow_status","6");
        int status_6 = borrowApplyService.listByMap(map).size();
        map1.put("borrow_status","2");
        int status_2 = borrowApplyService.listByMap(map1).size();
        map1.put("borrow_status","4");
        int status_4 = borrowApplyService.listByMap(map1).size();
        map.put("borrow_status","8");
        int status_8 = borrowApplyService.listByMap(map).size();
        if (hasRole(roleList,33)){
            result.put("applying",status_1);
            result.put("downloading",status_6);
            result.put("complete",status_8);
        }else{
            result.put("applying",'-');
            result.put("downloading",'-');
            result.put("complete",'-');
        }
        if (hasRole(roleList,34)){
            result.put("examining",status_1_noId);
        }else if (hasRole(roleList,35)){
            result.put("examining",status_2);
        }else if (hasRole(roleList,36)){
            result.put("examining",status_4);
        }else {
            result.put("examining",'-');
        }
        List list = new ArrayList();
        list.add(result);
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setMsg("获取成功").setData(list).toJSON();
    }

    @Override
    public String statisticsByUser(String userId,Map map) {
        String queryType = map.get("queryType").toString();//1 常规 2 zjl0754
        String queryDateType = map.get("queryDateType").toString();//1 年 2 月 3 日
        String queryDate = "";
        if (queryDateType.equals("1")){
            queryDate = DateUtils.getNowTimeStringToDD("yyyy");
        }
        if (queryDateType.equals("2")){
            queryDate = DateUtils.getNowTimeStringToDD("yyyy-MM");
        }
        if (queryDateType.equals("3")){
            queryDate = DateUtils.getNowTimeStringToDD("yyyy-MM-dd");
        }
        Page<Map> list= null;
        if (queryType.equals("1")){
            list = borrowApplyMapper.queryByDate(queryDate,userId);
        }else {
            list = borrowApplyZjl0754Mapper.queryByDate(queryDate,userId);
        }
        List result = new ArrayList();
        if (queryDate.length()==4){
            for (int i=1;i<13;i++){
                Map data = new HashMap();
                if (i<10)
                    data.put("applyTime","0"+i);
                else
                    data.put("applyTime",i);
                data.put("num",getNum(list,queryDate+"-"+data.get("applyTime").toString()));
                result.add(data);
            }
        }
        if (queryDate.length()==10){
            for (int i=0;i<24;i++){
                Map data = new HashMap();
                if (i<10)
                    data.put("applyTime","0"+i);
                else
                    data.put("applyTime",i);
                data.put("num",getNum(list,queryDate+" "+data.get("applyTime").toString()));
                result.add(data);
            }
        }
        if (queryDate.length()==7){
            LocalDate localDate = LocalDate.now();
            LocalDate query = localDate.withYear(Integer.valueOf(queryDate.split("-")[0])).withMonth(Integer.valueOf(queryDate.split("-")[1]));
            for (int i=1;i<query.lengthOfMonth()+1;i++){
                Map data = new HashMap();
                if (i<10)
                    data.put("applyTime","0"+i);
                else
                    data.put("applyTime",i);
                data.put("num",getNum(list,queryDate+"-"+data.get("applyTime").toString()));
                result.add(data);
            }
        }
        if (result!=null) {
            return new ResultMsg<>().setCode(ResultState.SUCCESS).setMsg("查询成功").setData(result).toJSON();
//            return new ResultMsg<>(ResultState.SUCCESS, null, JSONObject.toJSONString(list), list.getPageNum(), list.getTotal(), list.getPageSize(), list.getPages()).toPageJSON();
        }
        return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("无数据").toJSON();
    }
    private Object getNum(Page<Map> list, String applyTime) {
        if (list==null||list.getResult().size()==0){
            return 0;
        }
        for (int i = 0; i < list.getResult().size(); i++) {
            if (list.getResult().get(i).get("applyTime").toString().equals(applyTime)){
                return list.getResult().get(i).get("num");
            }
        }
        return 0;
    }

    @Override
    public String missionByUser(String userId) {
        List<Integer> roleList = userMapper.getRoleByUserId(userId);
        Page<Map> list = null;
        if (hasRole(roleList,33)){
            Map params = new HashMap();
            params.put("userId",userId);
            list = borrowApplyMapper.getListForMission(params);
        }else{
        }
        if (hasRole(roleList,34)){
            Map params1 = new HashMap();
            params1.put("borrowStatus","1");
            list = borrowApplyMapper.getListForMission(params1);
        }else if (hasRole(roleList,35)){
            Map params1 = new HashMap();
            params1.put("borrowStatus","2");
            list = borrowApplyMapper.getListForMission(params1);
        }else if (hasRole(roleList,36)){
            Map params1 = new HashMap();
            params1.put("borrowStatus","4");
            list = borrowApplyMapper.getListForMission(params1);
        }else {
        }
        if (list!=null) {
            return new ResultMsg<>(ResultState.SUCCESS, null, JSONObject.toJSONString(list), list.getPageNum(), list.getTotal(), list.getPageSize(), list.getPages()).toPageJSON();
        }
        return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("无数据").toJSON();
    }

    public boolean hasRole(List<Integer> roleList,int role_id ){
        for (int i = 0; i < roleList.size(); i++) {
            if ((""+roleList.get(i)).equals(""+(role_id))){
                return true;
            }
        }
        return false;
    }

}
