package com.ylcm.sys.excel.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.ylcm.sys.common.DateUtil;
import com.ylcm.sys.common.Util;
import com.ylcm.sys.domain.AdTheatre;
import com.ylcm.sys.domain.AdTheatreTv;
import com.ylcm.sys.excel.converter.FloatConverter;
import com.ylcm.sys.excel.converter.IntegerConverter;
import com.ylcm.sys.excel.converter.LocalDateConverter;
import com.ylcm.sys.excel.converter.LocalTimeConverter;
import com.ylcm.sys.vo.AdTheatreVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;

/**
 * 剧场数据（excel）
 * @author liuyu
 */
@Data
public class AdTheatreExcelDTO implements Serializable {
    private static final long serialVersionUID = 1859303387721713529L;

    @ApiModelProperty(value = "剧名")
    @ExcelProperty(index = 0, value="剧名")
    private String name;

    @ApiModelProperty(value = "集数")
    @ExcelProperty(index = 1, value="集数", converter = IntegerConverter.class)
    private Integer mediaNum;

    @ApiModelProperty(value = "媒体")
    @ExcelProperty(index = 2, value="媒体")
    private String mediaName;

    @ApiModelProperty(value = "进线预估（元）")
    @ExcelProperty(index = 3, value="进线预估（元）", converter = IntegerConverter.class)
    private Integer jinxian;

    @ApiModelProperty(value = "播出日期")
    @ExcelProperty(index = 4, value="播出日期", converter = LocalDateConverter.class)
    private LocalDate showDate;

    @ApiModelProperty(value = "播出时间")
    @ExcelProperty(index = 5, value="播出时间", converter = LocalTimeConverter.class)
    private LocalTime showTime;

    @ApiModelProperty(value = "类型")
    @ExcelProperty(index = 6, value="类型")
    private String type;

    @ApiModelProperty(value = "产品/栏目名")
    @ExcelProperty(index = 7, value="产品/栏目名")
    private String typeName;

    @ApiModelProperty(value = "时长（分）")
    @ExcelProperty(index = 8, value="时长（分）", converter = FloatConverter.class)
    private Float duration;

    @ApiModelProperty(value = "29省网收视率")
    @ExcelProperty(index = 9, value="29省网收视率", converter = FloatConverter.class)
    private Float rating29;

    @ApiModelProperty(value = "到达千人")
    @ExcelProperty(index = 10, value="到达千人", converter = IntegerConverter.class)
    private Integer dk29;

    @ApiModelProperty(value = "广东")
    @ExcelProperty(index = 11, value="广东", converter = IntegerConverter.class)
    private Integer guangdong;

    @ApiModelProperty(value = "北京")
    @ExcelProperty(index = 12, value="北京", converter = IntegerConverter.class)
    private Integer beijing;

    @ApiModelProperty(value = "上海")
    @ExcelProperty(index = 13, value="上海", converter = IntegerConverter.class)
    private Integer shanghai;

    @ApiModelProperty(value = "宁夏")
    @ExcelProperty(index = 14, value="宁夏", converter = IntegerConverter.class)
    private Integer ningxia;

    @ApiModelProperty(value = "浙江")
    @ExcelProperty(index = 15, value="浙江", converter = IntegerConverter.class)
    private Integer zhejiang;

    @ApiModelProperty(value = "江苏")
    @ExcelProperty(index = 16, value="江苏", converter = IntegerConverter.class)
    private Integer jiangsu;

    @ApiModelProperty(value = "湖南")
    @ExcelProperty(index = 17, value="湖南", converter = IntegerConverter.class)
    private Integer hunan;

    @ApiModelProperty(value = "湖北")
    @ExcelProperty(index = 18, value="湖北", converter = IntegerConverter.class)
    private Integer hubei;

    @ApiModelProperty(value = "四川")
    @ExcelProperty(index = 19, value="四川", converter = IntegerConverter.class)
    private Integer sichuan;

    @ApiModelProperty(value = "山东")
    @ExcelProperty(index = 20, value="山东", converter = IntegerConverter.class)
    private Integer shandong;

    @ApiModelProperty(value = "海南")
    @ExcelProperty(index = 21, value="海南", converter = IntegerConverter.class)
    private Integer hainan;

    @ApiModelProperty(value = "新疆")
    @ExcelProperty(index = 22, value="新疆", converter = IntegerConverter.class)
    private Integer xinjiang;

    @ApiModelProperty(value = "河北")
    @ExcelProperty(index = 23, value="河北", converter = IntegerConverter.class)
    private Integer hebei;

    @ApiModelProperty(value = "江西")
    @ExcelProperty(index = 24, value="江西", converter = IntegerConverter.class)
    private Integer jiangxi;

    @ApiModelProperty(value = "安徽")
    @ExcelProperty(index = 25, value="安徽", converter = IntegerConverter.class)
    private Integer anhui;

    @ApiModelProperty(value = "河南")
    @ExcelProperty(index = 26, value="河南", converter = IntegerConverter.class)
    private Integer henan;

    @ApiModelProperty(value = "陕西")
    @ExcelProperty(index = 27, value="陕西", converter = IntegerConverter.class)
    private Integer shaanxi;

    @ApiModelProperty(value = "吉林")
    @ExcelProperty(index = 28, value="吉林", converter = IntegerConverter.class)
    private Integer jilin;

    @ApiModelProperty(value = "重庆")
    @ExcelProperty(index = 29, value="重庆", converter = IntegerConverter.class)
    private Integer chongqing;

    @ApiModelProperty(value = "黑龙江")
    @ExcelProperty(index = 30, value="黑龙江", converter = IntegerConverter.class)
    private Integer heilongjiang;

    @ApiModelProperty(value = "广西")
    @ExcelProperty(index = 31, value="广西", converter = IntegerConverter.class)
    private Integer guangxi;

    @ApiModelProperty(value = "贵州")
    @ExcelProperty(index = 32, value="贵州", converter = IntegerConverter.class)
    private Integer guizhou;

    @ApiModelProperty(value = "云南")
    @ExcelProperty(index = 33, value="云南", converter = IntegerConverter.class)
    private Integer yunnan;

    @ApiModelProperty(value = "辽宁")
    @ExcelProperty(index = 34, value="辽宁", converter = IntegerConverter.class)
    private Integer liaoning;

    @ApiModelProperty(value = "内蒙古")
    @ExcelProperty(index = 35, value="内蒙古", converter = IntegerConverter.class)
    private Integer neimenggu;

    @ApiModelProperty(value = "山西")
    @ExcelProperty(index = 36, value="山西", converter = IntegerConverter.class)
    private Integer shanxi;

    @ApiModelProperty(value = "天津")
    @ExcelProperty(index = 37, value="天津", converter = IntegerConverter.class)
    private Integer tianjing;

    @ApiModelProperty(value = "甘肃")
    @ExcelProperty(index = 38, value="甘肃", converter = IntegerConverter.class)
    private Integer gansu;

    @ApiModelProperty(value = "福建")
    @ExcelProperty(index = 39, value="福建", converter = IntegerConverter.class)
    private Integer fujian;

    @ApiModelProperty(value = "男性比例")
    @ExcelProperty(index = 40, value="男性比例", converter = FloatConverter.class)
    private Float peopleMan29 = 0f;

    @ApiModelProperty(value = "女性比例")
    @ExcelProperty(index = 41, value="女性比例", converter = FloatConverter.class)
    private Float peopleWomen29;

    @ApiModelProperty(value = "4-14岁")
    @ExcelProperty(index = 42, value="4-14岁", converter = FloatConverter.class)
    private Float people41429;

    @ApiModelProperty(value = "15-24岁")
    @ExcelProperty(index = 43, value="15-24岁", converter = FloatConverter.class)
    private Float people152429;

    @ApiModelProperty(value = "25-34岁")
    @ExcelProperty(index = 44, value="25-34岁", converter = FloatConverter.class)
    private Float people253429;

    @ApiModelProperty(value = "35-44岁")
    @ExcelProperty(index = 45, value="35-44岁", converter = FloatConverter.class)
    private Float people354429;

    @ApiModelProperty(value = "45-54岁")
    @ExcelProperty(index = 46, value="45-54岁", converter = FloatConverter.class)
    private Float people455429;

    @ApiModelProperty(value = "55-64岁")
    @ExcelProperty(index = 47, value="55-64岁", converter = FloatConverter.class)
    private Float people556429;

    @ApiModelProperty(value = "65+岁")
    @ExcelProperty(index = 48, value="65+岁", converter = FloatConverter.class)
    private Float people6529;

    @ApiModelProperty(value = "51城市网收视率")
    @ExcelProperty(index = 49, value="51城市网收视率", converter = FloatConverter.class)
    private Float rating51;

    @ApiModelProperty(value = "到达千人")
    @ExcelProperty(index = 50, value="到达千人", converter = IntegerConverter.class)
    private Integer dk51;

    @ApiModelProperty(value = "男性比例")
    @ExcelProperty(index = 51, value="男性比例", converter = FloatConverter.class)
    private Float peopleMan51;

    @ApiModelProperty(value = "女性比例")
    @ExcelProperty(index = 52, value="女性比例", converter = FloatConverter.class)
    private Float peopleWoman51;

    @ApiModelProperty(value = "1-14岁")
    @ExcelProperty(index = 53, value="1-14岁", converter = FloatConverter.class)
    private Float people11451;

    @ApiModelProperty(value = "15-24岁")
    @ExcelProperty(index = 54, value="15-24岁", converter = FloatConverter.class)
    private Float people152451;

    @ApiModelProperty(value = "25-34岁")
    @ExcelProperty(index = 55, value="25-34岁", converter = FloatConverter.class)
    private Float people253451;

    @ApiModelProperty(value = "35-44岁")
    @ExcelProperty(index = 56, value="35-44岁", converter = FloatConverter.class)
    private Float people354451;

    @ApiModelProperty(value = "45-54岁")
    @ExcelProperty(index = 57, value="45-54岁", converter = FloatConverter.class)
    private Float people455451;

    @ApiModelProperty(value = "55-64岁")
    @ExcelProperty(index = 58, value="55-64岁", converter = FloatConverter.class)
    private Float people556451;

    @ApiModelProperty(value = "65+岁")
    @ExcelProperty(index = 59, value="65+岁", converter = FloatConverter.class)
    private Float people6551;

    /**
     * 数据验证
     * @return 错误时的信息
     */
    public String check() {
        /*
         * 必填项
         */
        if (StringUtils.isBlank(name)) {
            return "剧名不能为空";
        }
        if (mediaNum == null) {
            return "集数不能为空，或格式不对";
        }
        if (StringUtils.isBlank(mediaName)) {
            return "媒体不能为空";
        }
        if (jinxian != null && jinxian.toString().length() > 7){
            return "进线预估最多七位数";
        }
        if (showDate == null) {
            return "播出日期为空,或格式不对";
        }
        if (showTime == null) {
            return "播出时间为空,或格式不对";
        }
		if (duration != null && ( !Util.isNumericKeepOneDecimal(duration.toString())
				|| duration >= 100000)) {
			return "请填写正确的播放时长，只允许一位小数，最大不能超过5位数！";	
		}
        /*
         * 联合非空
         */
        if (StringUtils.isBlank(type) && StringUtils.isNotBlank(typeName)){
            return "“产品/栏目”类型为空";
        }

        /*
         * 检查百分比数值
         */
        return checkPercent();
    }

    /**
     * 检查百分比数值
     * @return errorMsg
     */
    public String checkPercent(){
        Float percent100 = 100f;
        // 29省网-男女比例
        Float temp = add(peopleMan29, peopleWomen29);
        if (temp != 0 && !temp.equals(percent100)){
            return "29省网男女百分比之和应为100";
        }
        // 51城市-男女比例
        temp = add(peopleMan51, peopleWoman51);
        if (temp != 0 && !temp.equals(percent100)){
            return "51城市男女百分比之和应为100";
        }

        // 数据格式化
        format();
        return null;
    }

    /**
     * 数据格式化<br/>
     * 收视率保留四位小数，百分比保留两位小数
     */
    public void format(){
        // 29省网-收视率
        rating29 = (rating29 == null ? null
                : new BigDecimal("" + rating29).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
        // 29省网-男女比例（保留两位小数）
        peopleMan29 = (peopleMan29 == null ? null
                : new BigDecimal("" + peopleMan29).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        peopleWomen29 = (peopleWomen29 == null ? null
                : new BigDecimal("" + peopleWomen29).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        // 29省网-各年龄段比例
        people41429 = (people41429 == null ? null
                : new BigDecimal("" + people41429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people152429 = (people152429 == null ? null
                : new BigDecimal("" + people152429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people253429 = (people253429 == null ? null
                : new BigDecimal("" + people253429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people354429 = (people354429 == null ? null
                : new BigDecimal("" + people354429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people455429 = (people455429 == null ? null
                : new BigDecimal("" + people455429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people556429 = (people556429 == null ? null
                : new BigDecimal("" + people556429).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people6529 = (people6529 == null ? null
                : new BigDecimal("" + people6529).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        // 51城市-收视率
        rating51 = (rating51 == null ? null
                : new BigDecimal("" + rating51).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
        // 51城市-男女
        peopleMan51 = (peopleMan51 == null ? null
                : new BigDecimal("" + peopleMan51).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        peopleWoman51 = (peopleWoman51 == null ? null
                : new BigDecimal("" + peopleWoman51).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        // 51城市-各年龄段
        people11451 = (people11451 == null ? null
                : new BigDecimal("" + people11451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people152451 = (people152451 == null ? null
                : new BigDecimal("" + people152451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people253451 = (people253451 == null ? null
                : new BigDecimal("" + people253451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people354451 = (people354451 == null ? null
                : new BigDecimal("" + people354451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people455451 = (people455451 == null ? null
                : new BigDecimal("" + people455451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people556451 = (people556451 == null ? null
                : new BigDecimal("" + people556451).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        people6551 = (people6551 == null ? null
                : new BigDecimal("" + people6551).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
    }

    /**
     * 多个 float 相加
     * @param arr 加数
     * @return 之和
     */
    private static float add(Float... arr){
        BigDecimal result = BigDecimal.ZERO;
        if (arr == null || arr.length <= 0) {
            return result.floatValue();
        }
        for (Float v: arr){
            if (v == null){
                continue;
            }
            result = result.add(new BigDecimal(v.toString()));
        }
        return result.floatValue();
    }

    public AdTheatre toAdTheatre(){
        AdTheatre adTheatre = new AdTheatre();
        BeanUtils.copyProperties(this, adTheatre);
        // 类型：0产品 1栏目
        if ("产品".equals(type)){
            adTheatre.setType(0);
        } else if ("栏目".equals(type)){
            adTheatre.setType(1);
        }
        return adTheatre;
    }

    public AdTheatreTv toAdTheatreTv(){
        AdTheatreTv tv = new AdTheatreTv();
        BeanUtils.copyProperties(this, tv);
        return tv;
    }

    @Data
    public static class AdTheatreExcelDTOBuilder{
        /**
         * 剧场数据
         */
        private AdTheatreVO adTheatre;
        /**
         * 电视剧收视数据
         */
        private AdTheatreTv adTheatreTv;

        public AdTheatreExcelDTO build(){
            /*
             * 剧场数据
             */
            AdTheatreExcelDTO excelDTO = new AdTheatreExcelDTO();
            BeanUtils.copyProperties(adTheatre, excelDTO);
            if (StringUtils.isNotBlank(adTheatre.getShowDate())){
                excelDTO.setShowDate(DateUtil.str2Date(adTheatre.getShowDate()));
            }
            if (StringUtils.isNotBlank(adTheatre.getShowTime())){
                excelDTO.setShowTime(LocalTime.parse(adTheatre.getShowTime()));
            }
            // 类型：0产品 1栏目
            if (adTheatre.getType() != null){
                excelDTO.setType(adTheatre.getType() == 0 ? "产品" : "栏目");
            }

            /*
             * 电视剧收视率数据
             */
            if (adTheatreTv != null){
                BeanUtils.copyProperties(adTheatreTv, excelDTO);
            }
            return excelDTO;
        }

        public AdTheatreExcelDTOBuilder adTheatre(AdTheatreVO adTheatre){
            this.adTheatre = adTheatre;
            return this;
        }

        public AdTheatreExcelDTOBuilder adTheatreTv(AdTheatreTv adTheatreTv){
            this.adTheatreTv = adTheatreTv;
            return this;
        }
    }
}
