package com.ylcm.sys.excel.model;

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

import javax.validation.constraints.NotNull;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.alibaba.excel.annotation.ExcelProperty;
import com.ylcm.sys.common.DateUtil;
import com.ylcm.sys.common.EnumHelperUtil;
import com.ylcm.sys.common.Util;
import com.ylcm.sys.domain.AdSpecial;
import com.ylcm.sys.enums.AdSpecialShowEnvEnum;
import com.ylcm.sys.excel.converter.LocalDateConverter;
import com.ylcm.sys.excel.converter.LocalTimeConverter;
import com.ylcm.sys.form.AdSpecialForm;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class AdSpecialProductImportDTO implements Serializable{

	/**
	 *   产品导入
	 */
	private static final long serialVersionUID = 3863153959765813542L;

    @NotNull
    @ApiModelProperty(value = "产品名称")
    @ExcelProperty(index = 0)
    private String productName;

    @ApiModelProperty(value = "品牌")
    @ExcelProperty(index = 1)
    private String brandName;
    
    @ApiModelProperty(value = "广告主")
    @ExcelProperty(index = 2)
    private String advMaster;

    @NotNull
    @ApiModelProperty(value = "媒体名称")
    @ExcelProperty(index = 3)
    private String mediaName;

    @NotNull
    @ApiModelProperty(value = "播出环境 0电视剧 1综艺 2广告")
    @ExcelProperty(index = 4)
    private String showEnv;

    @NotNull
    @ApiModelProperty(value = "播出节目")
    @ExcelProperty(index = 5)
    private String tvName;

    @NotNull
    @ApiModelProperty(value = "播出日期")
    @ExcelProperty(index = 6, converter = LocalDateConverter.class)
    private LocalDate showDate;
    
    @NotNull
    @ApiModelProperty(value = "播出时间")
    @ExcelProperty(index = 7, converter = LocalTimeConverter.class)
    private LocalTime showTime;
    
    @ApiModelProperty(value = "联系方式")
    @ExcelProperty(index = 8)
    private String phone = "";
    
    @ApiModelProperty(value = "播出时长(单位分)")
    @ExcelProperty(index = 9)
    private String showDuration;
    
    @ApiModelProperty(value = "刊例(万）")
    @ExcelProperty(index = 10)
    private String price;
    
    private Integer type;
    
    private String msg = "";

    public void check() {
    	if (StringUtils.isBlank(productName)) {
			msg = "栏目名称不能为空";
			return;
		}
    	if (StringUtils.isBlank(mediaName)) {
			msg = "媒体名称不能为空";
			return;
		}
    	if (StringUtils.isBlank(showEnv)) {
			msg = "播出环境不能为空";
			return;
		}
    	if (StringUtils.isBlank(tvName)) {
			msg = "播出节目不能为空";
			return;
		}
    	if (showDate == null) {
			msg = "播出日期不能为空或者格式不对";
			return;
		}
    	if (showTime == null) {
			msg = "播出时间不能为空或者格式不对";
			return;
		}
    	if (StringUtils.isBlank(showDuration)) {
			msg = "播放时长不能为空";
			return;
		}
     	if (StringUtils.isNotBlank(advMaster) && advMaster.length() > 20) {
    	    msg = "广告主长度不能超过20个字";
    	    return;
    	}
		if (!showEnv.contains("电视剧") && tvName.length() > 20) {
			msg = "播出节目长度不能超过20个字";
			return;
		}
		if (StringUtils.isNotBlank(phone) && phone.length() > 20) {
			msg = "联系方式长度不能超过20个字";
			return;
		}
    	if (StringUtils.isNotBlank(showDuration) && (showDuration.length() > 6 || !Util.isNumericKeepOneDecimal(showDuration)
    			|| Float.parseFloat(showDuration) >= 100000F)) {
			msg = "请填写正确的播放时长，只允许一位小数，最大不能超过5位数！";
			return;		
		}
    	if (StringUtils.isNotBlank(price) && (price.length() > 8 || !Util.isNumeric(price,false))) {
			msg = "请填写正确的刊例价格，最大不能超过8位数！";
			return;
		}
    }
    
    public AdSpecial toSpecial() {
    	AdSpecial adSpecial = new AdSpecial();
    	BeanUtils.copyProperties(this, adSpecial);
    	
    	if (StringUtils.isNotBlank(price)) {
			adSpecial.setPrice(new BigDecimal(price));
		}
    	if (StringUtils.isNotBlank(showDuration)) {
			adSpecial.setShowDuration(Float.parseFloat(showDuration));
		}
    	AdSpecialShowEnvEnum showEnvEnum = EnumHelperUtil.getOneByName(AdSpecialShowEnvEnum.class, showEnv);
    	adSpecial.setShowEnv(showEnvEnum.getCode());
    	
    	return adSpecial;
    }
    
    public AdSpecialForm toSpecialForm(AdSpecial adSpecial) {
    	AdSpecialForm adSpecialForm = new AdSpecialForm();
    	BeanUtils.copyProperties(adSpecial, adSpecialForm);
    	adSpecialForm.setShowDate(DateUtil.getStringByLocalDate(showDate));
    	adSpecialForm.setShowTime(DateUtil.getStringByLocalTime(showTime));
    	return adSpecialForm;
    }
}
