package com.chy.borrow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chy.borrow.entity.BorrowApplyZjl0754;
import com.chy.borrow.mapper.BorrowApplyMapper;
import com.chy.borrow.mapper.BorrowApplyZjl0754Mapper;
import com.chy.borrow.service.BorrowApplyZjl0754Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanghaigeography.entity.ResultMsg;
import com.shanghaigeography.eum.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沈晔浩
 * @since 2022-03-25
 */
@Service
public class BorrowApplyZjl0754ServiceImpl extends ServiceImpl<BorrowApplyZjl0754Mapper, BorrowApplyZjl0754> implements BorrowApplyZjl0754Service {

    @Autowired
    BorrowApplyZjl0754Mapper borrowApplyZjl0754Mapper;

    @Override
    public String getList(Map map) {
        try {
            Integer current = Integer.valueOf(map.get("current").toString());
            Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
            map.put("borrowStatus","6");
            PageHelper.startPage(current, pageSize);
            Page<Map> list = borrowApplyZjl0754Mapper.getList(map);
            if (list!=null) {
                return new ResultMsg<>(ResultState.SUCCESS, null, JSONObject.toJSONString(list), list.getPageNum(), list.getTotal(), list.getPageSize(), list.getPages()).toPageJSON();
            }
        }catch (Exception e){
            return new ResultMsg<String>().setCode(ResultState.ERROR).setMsg("服务器出错").toJSON();
        }
        return new ResultMsg<String>().setCode(ResultState.SUCCESS).setMsg("无数据").toJSON();
    }

    @Override
    public String getInfoById(BorrowApplyZjl0754 borrowApplyZjl0754) {
        Map map1 = borrowApplyZjl0754Mapper.getInfoById(borrowApplyZjl0754.getBorrowApplyId());
        List result = new ArrayList();
        if (map1!=null){
            result.add(map1);
        }
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(result).toJSON();
    }

    //借阅量（zjl0754）
    @Override
    public String getCountListzj() {
        List<Map> countList = borrowApplyZjl0754Mapper.getCountListzj();

        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(countList).toJSON();
    }

    @Override
    public String getApplyTimeCountList(Integer start, Integer num) {
        List<Map> timeCountList = borrowApplyZjl0754Mapper.getApplyTimeCountList(start, num);
        List<Map> result = new ArrayList<>();
        LocalDate date = LocalDate.now();
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastDay.getDayOfMonth());
        int MonthDay=lastDay.getDayOfMonth();
        //年填充字段
        if (start == 0 && num == 0) {
            for (int i = 1; i <= 12; i++) {
                Map<String,Integer> temp = new HashMap();
                temp.put("applyTime", i);
                temp.put("borrowCount", 0);
                result.add(temp);

            }
        }
        //日填充字段
        else if(start == 6 && num == 6){
            for (int i = 1; i <= 24; i++) {
                Map<String,Integer> temp = new HashMap();
                temp.put("applyTime", i);
                temp.put("borrowCount", 0);
                result.add(temp);
            }
        }
        //月填充字段
        else if(start == 3 && num == 3){
            for (int i = 1; i <=MonthDay ; i++) {
                Map<String,Integer> temp = new HashMap();
                temp.put("applyTime", i);
                temp.put("borrowCount", 0);
                result.add(temp);
            }
        }
        for (Map<String,String> data : timeCountList){

            Integer idx=Integer.valueOf(data.get("applyTime"));

            if (Integer.valueOf(data.get("applyTime")) == idx) {
                Map map = result.get(idx - 1);

                map.put("borrowCount",data.get("borrowCount"));
            }

        }
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(result).toJSON();
    }

    @Override
    public String getApplyTimeByPeriod(Timestamp start, Timestamp end) {
        List<Map> TimeByPeriodList = borrowApplyZjl0754Mapper.getApplyTimeByPeriod(start,end);
        return new ResultMsg<>().setCode(ResultState.SUCCESS).setData(TimeByPeriodList).toJSON();
    }
}
