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.EnumHelperUtil;
import com.ylcm.sys.domain.AdProduct;
import com.ylcm.sys.domain.AdSpecial;
import com.ylcm.sys.domain.AdTv;
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 com.ylcm.sys.service.AdProductService;
import com.ylcm.sys.service.AdTvService;

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

@Data
public class AdSpecialImportDTO implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3863153959765813542L;

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

//    @ApiModelProperty(value = "品牌")
//    @ExcelProperty(index = 0)
//    private String brandName;

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

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

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

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

    @NotNull
    @ApiModelProperty(value = "播出日期")
    @ExcelProperty(index = 5, converter = LocalDateConverter.class)
    private LocalDate showDate;
    
    @NotNull
    @ApiModelProperty(value = "播出时间")
    @ExcelProperty(index = 6, converter = LocalTimeConverter.class)
    private LocalTime showTime;
    
    @ApiModelProperty(value = "联系方式")
    @ExcelProperty(index = 7)
    private String phone = "";
    
    @ApiModelProperty(value = "播出时长(单位秒)")
    @ExcelProperty(index = 8)
    private String showDuration;
    
    @ApiModelProperty(value = "价格")
    @ExcelProperty(index = 9)
    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;
		}
    }
    
    public AdSpecial toSpecial(AdProductService adProductService) {
    	AdSpecial adSpecial = new AdSpecial();
    	BeanUtils.copyProperties(this, adSpecial);

//    	adSpecial.setShowDate(DateUtil.getLocaDateByString(showDate));
//    	adSpecial.setShowTime(showTime.toLocalTime());
    	
    	if (StringUtils.isNotBlank(price)) {
			adSpecial.setPrice(new BigDecimal(price));
		}
    	if (StringUtils.isNotBlank(showDuration)) {
			adSpecial.setShowDuration(Integer.parseInt(showDuration));
		}
    	
    	AdSpecialShowEnvEnum showEnvEnum = EnumHelperUtil.getOneByName(AdSpecialShowEnvEnum.class, showEnv);
    	adSpecial.setShowEnv(showEnvEnum.getCode());
    	
    	AdProduct adProduct = adProductService.getByName(productName);
    	if (adProduct != null) {
			adSpecial.setBrandName(adProduct.getBrandName());
		}
    	
    	return adSpecial;
    }
    
    public AdSpecialForm toSpecialForm() {
    	AdSpecialForm adSpecial = new AdSpecialForm();
    	BeanUtils.copyProperties(this, adSpecial);

    	return adSpecial;
    }
}
