package com.ylcm.sys.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ylcm.sys.common.Constants;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.domain.AdTheatreTv;
import com.ylcm.sys.domain.AdTheatreTvStat;
import com.ylcm.sys.enums.ProvinceEnum;
import com.ylcm.sys.service.AdTheatreService;
import com.ylcm.sys.service.AdTheatreTvService;
import com.ylcm.sys.service.AdTheatreTvStatService;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.core.Local;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 关联电视剧数据 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-01
 */
@RestController
@Slf4j
@RequestMapping("/ad-theatre-tv-stat")
public class AdTheatreTvStatController extends BaseController{

    @Resource
    private AdTheatreTvStatService adTheatreTvStatService;

    @Resource
    private AdTheatreService adTheatreService;

    @Resource
    private AdTheatreTvService adTheatreTvService;


    @RequestMapping(value = "/random_batch.do")
	public void del(Integer id,
                    HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		result.put("code", Constants.CODE_SUCCESS);
		result.put("msg", "服务器异常");

        List<String> tvNames = Arrays.asList("神医喜来乐1", "康熙微服私访2", "亮剑3", "小娘惹", "河神2", "局中人", "怪你过分美丽",
                "义道", "哪吒", "湾区儿女", "幸福触手可及", "三叉戟", "蜗牛与黄鹂鸟", "觉醒", "隐秘的角落",
                "你是我的命中注定", "夏夜知君暖", "塞上风云集", "爱的复仇", "法定战服", "美林男孩", "少年游",
                "传闻中的陈芊芊", "锦绣南歌", "亲爱的义祁君", "奈何boss又如何", "燃烧", "时间倒数遇见你2", "山寨小萌主", "庆余年第一季",
                "长相守", "三千鸦杀", "三千鸦杀", "你成功引起我的注意了", "民国奇探", "唐人街探案", "十日游戏", "宸汐缘");
        List<String> mediaNames = Arrays.asList("浙江卫视", "山东卫视", "北京卫视", "湖南卫视", "广西卫视", "广东卫视",
                "江西卫视", "江苏卫视", "湖北卫视", "湖南卫视", "河南卫视", "河北卫视", "宁夏卫视", "新疆卫视",
                "西藏卫视", "云南卫视", "四川卫视", "黑龙江卫视", "内蒙古卫视", "海南卫视", "甘肃卫视", "辽宁卫视", "吉林卫视", "陕西卫视",
                "山西卫视", "安徽卫视", "福建卫视");
        List<LocalDate> showDates = Arrays.asList(LocalDate.parse("2020-08-01"), LocalDate.parse("2020-08-02"), LocalDate.parse("2020-08-03"),
                LocalDate.parse("2020-08-04"), LocalDate.parse("2020-08-05"), LocalDate.parse("2020-08-06"),
                LocalDate.parse("2020-08-07"), LocalDate.parse("2020-08-08"), LocalDate.parse("2020-08-09"),
                LocalDate.parse("2020-08-10"), LocalDate.parse("2020-08-11"), LocalDate.parse("2020-08-12"),
                LocalDate.parse("2020-08-13"), LocalDate.parse("2020-08-14"), LocalDate.parse("2020-08-15"),
                LocalDate.parse("2020-08-16"), LocalDate.parse("2020-08-17"), LocalDate.parse("2020-08-18"),
                LocalDate.parse("2020-08-19"), LocalDate.parse("2020-08-20"), LocalDate.parse("2020-08-21"),
                LocalDate.parse("2020-08-22"), LocalDate.parse("2020-08-23"), LocalDate.parse("2020-08-24"),
                LocalDate.parse("2020-08-25"), LocalDate.parse("2020-08-26"), LocalDate.parse("2020-08-27"));

		for (int i=0; i<10000; i++) {
            AdTheatreTvStat adTheatreTvStat = new AdTheatreTvStat();

            int random = RandomUtils.nextInt(tvNames.size());
            adTheatreTvStat.setName(tvNames.get(random));

            random = RandomUtils.nextInt(mediaNames.size());
            adTheatreTvStat.setMediaName(mediaNames.get(random));

            adTheatreTvStat.setFirstTheme("古装");
            adTheatreTvStat.setSecondTheme("传奇");
            adTheatreTvStat.setTheatreTime(RandomUtils.nextInt(4));
            adTheatreTvStat.setChannelSystem(1);
            adTheatreTvStat.setSeason("2020-2Q");
            adTheatreTvStat.setTvNum(1);
            adTheatreTvStat.setRating29(RandomUtils.nextFloat());
            adTheatreTvStat.setRating51(RandomUtils.nextFloat());
            adTheatreTvStat.setDk29(RandomUtils.nextInt(10000));
            adTheatreTvStat.setDk51(RandomUtils.nextInt(10000));

            random = RandomUtils.nextInt(showDates.size());
            adTheatreTvStat.setShowDate(showDates.get(random));

            AdTheatre adTheatre = new AdTheatre();
            BeanUtils.copyProperties(adTheatreTvStat, adTheatre);
            adTheatre.setType(0);
            adTheatre.setTypeName("28春收音机");
            if (adTheatreTvStat.getTheatreTime() == 0) {
                adTheatre.setShowTime(LocalTime.parse("12:30:00"));
            } else if (adTheatreTvStat.getTheatreTime() == 1) {
                adTheatre.setShowTime(LocalTime.parse("18:30:00"));
            } else if (adTheatreTvStat.getTheatreTime() == 2) {
                adTheatre.setShowTime(LocalTime.parse("22:30:00"));
            } else if (adTheatreTvStat.getTheatreTime() == 3) {
                adTheatre.setShowTime(LocalTime.parse("02:30:00"));
            }
            adTheatre.setJinxian(5);
            adTheatre.setMediaNum(i%50);

            AdTheatreTv adTheatreTv = new AdTheatreTv();
            for(ProvinceEnum e : ProvinceEnum.values()) {
                try {
                    MethodUtils.invokeMethod(adTheatreTv, "set" + e.getCode().substring(0,1).toUpperCase() + e.getCode().substring(1, e.getCode().length()),
                            new Object[]{RandomUtils.nextInt(10000)}, new Class[]{Integer.class});
                } catch (NoSuchMethodException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InvocationTargetException ex) {
                    ex.printStackTrace();
                }
            }
            adTheatreTv.setDk29(adTheatreTvStat.getDk29());
            adTheatreTv.setDk51(adTheatreTvStat.getDk51());
            adTheatreTv.setRating29(adTheatreTvStat.getRating29());
            adTheatreTv.setRating51(adTheatreTvStat.getRating51());
            adTheatreTv.setPeopleMan29(RandomUtils.nextFloat());
            adTheatreTv.setPeopleWomen29(RandomUtils.nextFloat());
            adTheatreTv.setPeopleMan51(RandomUtils.nextFloat());
            adTheatreTv.setPeopleWoman51(RandomUtils.nextFloat());

            LambdaQueryWrapper<AdTheatreTvStat> lambdaQueryWrapper = new LambdaQueryWrapper<AdTheatreTvStat>()
                    .eq(AdTheatreTvStat::getName, adTheatreTvStat.getName())
                    .eq(AdTheatreTvStat::getMediaName, adTheatreTvStat.getMediaName())
                    .eq(AdTheatreTvStat::getTheatreTime, adTheatreTvStat.getTheatreTime())
                    .eq(AdTheatreTvStat::getShowDate, adTheatreTvStat.getShowDate());
            AdTheatreTvStat domainIndb = adTheatreTvStatService.getOne(lambdaQueryWrapper);
            if (domainIndb == null) {
//                adTheatreTvStatService.save(adTheatreTvStat);
                adTheatreService.save(adTheatre);
                adTheatreTv.setAdTheatreId(adTheatre.getId());
                adTheatreTvService.add(adTheatreTv);
            } else {
                log.info(adTheatreTvStat.toString());
            }
        }

		returnResult(request, response, result);
	}

    @RequestMapping(value = "/upd.do")
    public void upd(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<AdTheatre> list = adTheatreService.list();
        for (AdTheatre adTheatre:list) {
            adTheatreTvStatService.saveStatByTheatreTv(adTheatre);
        }
    }
}
