package com.liquidnet.client.admin.web.controller.zhengzai.kylin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.kylin.dto.PerformanceCalendarReq;
import com.liquidnet.client.admin.zhengzai.kylin.dto.PerformanceCalendarResp;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.service.kylin.entity.KylinPerformances;
import com.liquidnet.service.kylin.service.admin.IKylinPerformancesAdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: PerformanceCalendar
 * @Package com.liquidnet.client.admin.web.controller.zhengzai.kylin
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/5/24 19:58
 */
@Controller
@RequestMapping("/kylin/performanceCalendar")
public class PerformanceCalendarController extends BaseController{
    private String prefix = "zhengzai/kylin/performanceCalendar";

    @Autowired
    private IKylinPerformancesAdminService kylinPerformancesAdminService;

    @RequiresPermissions("kylin:performanceCalendar:view")
    @GetMapping()
    public String performances(PerformanceCalendarReq performanceCalendarReq, ModelMap mmap)
    {
        performanceCalendarReq.setPerformanceIimeBegin("2021-05-31");
        performanceCalendarReq.setPerformanceIimeend("2021-06-06");
        int days = Long.valueOf(DateUtil.intervalDays(DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR)
                ,DateUtil.parse(performanceCalendarReq.getPerformanceIimeend(),DateUtil.DATE_SMALL_STR))).intValue() + 1;

        //查询条件
        LambdaQueryWrapper<KylinPerformances> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNotNull(performanceCalendarReq.getPerformanceIimeBegin())) {
            wrapper.ge(KylinPerformances::getTimeStart, DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR));
        }
        if (!StringUtils.isNotNull(performanceCalendarReq.getPerformanceIimeend())) {
            wrapper.le(KylinPerformances::getTimeStart, DateUtil.addDay(DateUtil.parse(performanceCalendarReq.getPerformanceIimeend(),DateUtil.DATE_SMALL_STR),1));
        }

        List<KylinPerformances> list = kylinPerformancesAdminService.list(wrapper);

        String[][] dateArray2 = getDateList(DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR),days);
        //初始化对象
        List<PerformanceCalendarResp> respList = initRespList(dateArray2);
        for (KylinPerformances kylinPerformances : list) {
            //定义演出对象
            PerformanceCalendarResp.PerformanceVo performanceVo = new PerformanceCalendarResp.PerformanceVo();
            performanceVo.setTitle(kylinPerformances.getTitle());
            performanceVo.setCityName(kylinPerformances.getCityName());
            performanceVo.setTimeStart(DateUtil.format(kylinPerformances.getTimeStart(),DateUtil.Formatter.ddHHmmssTrim));

            //页面行循环
            for(int i = 0;i< dateArray2.length;i++){
                //页面列循环
                for(int j = 0;j<dateArray2[i].length; j++){

                    String targetDate = dateArray2[i][j];
                    if(DateUtil.format(kylinPerformances.getTimeStart(),DateUtil.Formatter.yyyy_MM_dd).equalsIgnoreCase(targetDate)){
                        switch (j){
                            case 0:
                                List<PerformanceCalendarResp.PerformanceVo> monList = respList.get(i).getMonList();
                                monList.add(performanceVo);
                                break;
                            case 1:
                                List<PerformanceCalendarResp.PerformanceVo> tueList = respList.get(i).getTueList();
                                tueList.add(performanceVo);
                                break;
                            case 2:
                                List<PerformanceCalendarResp.PerformanceVo> wedList = respList.get(i).getWedList();
                                wedList.add(performanceVo);
                                break;
                            case 3:
                                List<PerformanceCalendarResp.PerformanceVo> thuList = respList.get(i).getThuList();
                                thuList.add(performanceVo);
                                break;
                            case 4:
                                List<PerformanceCalendarResp.PerformanceVo> friList = respList.get(i).getFriList();
                                friList.add(performanceVo);
                                break;
                            case 5:
                                List<PerformanceCalendarResp.PerformanceVo> satList = respList.get(i).getSatList();
                                satList.add(performanceVo);
                                break;
                            case 6:
                                List<PerformanceCalendarResp.PerformanceVo> sunList = respList.get(i).getSunList();
                                sunList.add(performanceVo);
                                break;
                            default:
                                break;

                        }
                    }
                }
            }
        }

        mmap.put("respDataList",respList);
        return prefix + "/performanceCalendar";
    }

    /**
     * 查询演出列表
     */
    @RequiresPermissions("kylin:performanceCalendar:list")
    @PostMapping("/list")
    public TableDataInfo list(PerformanceCalendarReq performanceCalendarReq, ModelMap mmap)
    {
        performanceCalendarReq.setPerformanceIimeBegin("2021-05-31");
        performanceCalendarReq.setPerformanceIimeend("2021-06-06");
        int days = Long.valueOf(DateUtil.intervalDays(DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR)
        ,DateUtil.parse(performanceCalendarReq.getPerformanceIimeend(),DateUtil.DATE_SMALL_STR))).intValue() + 1;

        //查询条件
        LambdaQueryWrapper<KylinPerformances> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNotNull(performanceCalendarReq.getPerformanceIimeBegin())) {
            wrapper.ge(KylinPerformances::getTimeStart, DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR));
        }
        if (!StringUtils.isNotNull(performanceCalendarReq.getPerformanceIimeend())) {
            wrapper.le(KylinPerformances::getTimeStart, DateUtil.addDay(DateUtil.parse(performanceCalendarReq.getPerformanceIimeend(),DateUtil.DATE_SMALL_STR),1));
        }

        List<KylinPerformances> list = kylinPerformancesAdminService.list(wrapper);

        String[][] dateArray2 = getDateList(DateUtil.parse(performanceCalendarReq.getPerformanceIimeBegin(),DateUtil.DATE_SMALL_STR),days);
        //初始化对象
        List<PerformanceCalendarResp> respList = initRespList(dateArray2);
        for (KylinPerformances kylinPerformances : list) {
            //定义演出对象
            PerformanceCalendarResp.PerformanceVo performanceVo = new PerformanceCalendarResp.PerformanceVo();
            performanceVo.setTitle(kylinPerformances.getTitle());
            performanceVo.setCityName(kylinPerformances.getCityName());
            performanceVo.setTimeStart(DateUtil.format(kylinPerformances.getTimeStart(),DateUtil.Formatter.ddHHmmssTrim));

            //页面行循环
            for(int i = 0;i< dateArray2.length;i++){
                //页面列循环
                for(int j = 0;j<dateArray2[i].length; j++){

                    String targetDate = dateArray2[i][j];
                    if(DateUtil.format(kylinPerformances.getTimeStart(),DateUtil.Formatter.yyyy_MM_dd).equalsIgnoreCase(targetDate)){
                        switch (j){
                            case 0:
                                List<PerformanceCalendarResp.PerformanceVo> monList = respList.get(i).getMonList();
                                monList.add(performanceVo);
                                break;
                            case 1:
                                List<PerformanceCalendarResp.PerformanceVo> tueList = respList.get(i).getTueList();
                                tueList.add(performanceVo);
                                break;
                            case 2:
                                List<PerformanceCalendarResp.PerformanceVo> wedList = respList.get(i).getWedList();
                                wedList.add(performanceVo);
                                break;
                            case 3:
                                List<PerformanceCalendarResp.PerformanceVo> thuList = respList.get(i).getThuList();
                                thuList.add(performanceVo);
                                break;
                            case 4:
                                List<PerformanceCalendarResp.PerformanceVo> friList = respList.get(i).getFriList();
                                friList.add(performanceVo);
                                break;
                            case 5:
                                List<PerformanceCalendarResp.PerformanceVo> satList = respList.get(i).getSatList();
                                satList.add(performanceVo);
                                break;
                            case 6:
                                List<PerformanceCalendarResp.PerformanceVo> sunList = respList.get(i).getSunList();
                                sunList.add(performanceVo);
                                break;
                            default:
                                break;

                        }
                    }
                }
            }
        }

        mmap.put("respDataList",respList);
        return getDataTable(respList);
    }

    /**
     * days必须是7的倍数 7，14
     * @param beginDate
     * @param days
     * @return
     */
    private String[][] getDateList(Date beginDate, int days){
        //获取所有日期
        List<String> dateList = new ArrayList<>();
        for(int i=0;i<days;i++){
            dateList.add(DateUtil.format(DateUtil.addDay(beginDate,i),DateUtil.Formatter.yyyy_MM_dd));
        }

        //获取显示行数
        int rowNum = days/7;
        String[][] strArray = new String[rowNum][7];

        for(int i=0; i<rowNum; i++){
            for(int j=0; j < 7; j++){
                strArray[i][j] = dateList.get(i * 7 + j);
            }
        }

        return strArray;
    }

    private List<PerformanceCalendarResp> initRespList(String[][] dateArray2){

        List<PerformanceCalendarResp> respList = new ArrayList<>();
        for(int i = 0;i< dateArray2.length;i++){
            PerformanceCalendarResp resp = new PerformanceCalendarResp();
            for(int j = 0;j<dateArray2[i].length; j++){
                String targetDate = dateArray2[i][j];
                    switch (j){
                        case 0:
                            resp.setMonList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 1:
                            resp.setTueList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 2:
                            resp.setWedList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 3:
                            resp.setThuList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 4:
                            resp.setFriList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 5:
                            resp.setSatList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        case 6:
                            resp.setSunList(new ArrayList<PerformanceCalendarResp.PerformanceVo>());
                            break;
                        default:
                            break;

                    }
            }
            respList.add(resp);
        }
        return respList;
    }

}
