package com.cool.store.request;

import com.cool.store.dto.TallyBookDetailDTO;
import com.cool.store.entity.TallyBookDO;
import com.cool.store.entity.TallyBookDetailDO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class TallyBookRequest {

    @ApiModelProperty(value = "主键id，修改时候传")
    @NotNull(message = "主键id不能为空")
    private Long id;

    @ApiModelProperty(value = "加盟商ID")
    private String partnerId;

    @ApiModelProperty(value = "店铺ID", required = true)
    @NotNull(message = "店铺ID不能为空")
    private Long shopId;

    @ApiModelProperty(value = "年", required = true)
    @NotNull(message = "年不能为空")
    private Integer year;

    @ApiModelProperty(value = "月", required = true)
    @NotNull(message = "月不能为空")
    private Integer month;

    @ApiModelProperty(value = "成本总计", required = true)
    @NotNull(message = "成本总计")
    private BigDecimal total;

    @ApiModelProperty(value = "明细")
    @NotEmpty(message = "明细不能为空")
    private List<TallyBookDetailDTO> tallyBookDetailDTOList;

    @ApiModelProperty(value = "操作人",hidden = true)
    private String operatorName;

    public TallyBookDO toTallyBookDO() {
        TallyBookDO tallyBookDO = new TallyBookDO();
        tallyBookDO.setPartnerId(partnerId);
        tallyBookDO.setShopId(shopId);
        tallyBookDO.setTotal(total);
        tallyBookDO.setYear(year);
        tallyBookDO.setMonth(month);
        return tallyBookDO;
    }
    public List<TallyBookDetailDO> toTallyBookDetailDOList(Long tallyBookId) {
        if (tallyBookId==null || CollectionUtils.isEmpty(tallyBookDetailDTOList))
        {
            return null;
        }
        return tallyBookDetailDTOList.stream().map(tallyBookDetailDTO -> {
            TallyBookDetailDO tallyBookDetailDO = new TallyBookDetailDO();
            tallyBookDetailDO.setTallyBookId(tallyBookId);
            tallyBookDetailDO.setFieldName(tallyBookDetailDTO.getFieldName());
            tallyBookDetailDO.setFieldValue(tallyBookDetailDTO.getFieldValue());
            tallyBookDetailDO.setCreateTime(new Date());
            return tallyBookDetailDO;
        }).collect(Collectors.toList());
    }


}