package com.ruoyi.clock.domain.vo;

import java.util.Date;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.clock.domain.Employee;
import com.ruoyi.clock.domain.bo.AgentBo;
import com.ruoyi.clock.service.IActivityAgentService;
import com.ruoyi.clock.service.IActivityProductService;
import com.ruoyi.clock.service.IAgentService;
import com.ruoyi.clock.service.IClockRecordService;
import com.ruoyi.common.annotation.ExcelDictFormat;
import com.ruoyi.common.convert.ExcelDictConvert;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.typehandler.ListToStringTypeHandler;
import com.ruoyi.common.utils.spring.SpringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 活动信息视图对象 tb_activity
 *
 * @author pengc
 * @date 2026-03-04
 */
@Data
@ApiModel("活动信息视图对象")
@ExcelIgnoreUnannotated
public class ActivityVo extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 活动id
     */
    @ApiModelProperty("活动id")
    private Long activityId;

    /**
     * 活动名称
     */
    @ExcelProperty(value = "活动名称")
    @ApiModelProperty("活动名称")
    private String activityName;

    /**
     * 活动类型 0晒单打卡 1销售pk
     */
    @ExcelProperty(value = "活动类型 0晒单打卡 1销售pk")
    @ApiModelProperty("活动类型 0晒单打卡 1销售pk")
    private Integer type;

    @ApiModelProperty(value = "代理商信息", required = true)
    private List<AgentVo> agentList;

    @ExcelProperty(value = "代理商名称")
    private String agentName;

    /**
     * 开始日期
     */
    @ExcelProperty(value = "开始日期")
    @ApiModelProperty("开始日期")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;

    /**
     * 结束日期
     */
    @ExcelProperty(value = "结束日期")
    @ApiModelProperty("结束日期")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;

    @ApiModelProperty(value = "代理商信息", required = true)
    private List<ProductVo> productList;

    @ExcelProperty(value = "产品信息")
    private String productName;

    /**
     * 活动图片
     */
    @ApiModelProperty("活动图片")
    @TableField(typeHandler = ListToStringTypeHandler.class)
    private List<String> image;

    @ExcelProperty(value = "图片信息")
    private String imageUrl;

    /**
     * 单位 0 盒 1 支
     */
    @ExcelProperty(value = "单位 0 盒 1 支")
    @ApiModelProperty("单位 0 盒 1 支")
    private Integer unit;

    /**
     * 活动规则
     */
    @ExcelProperty(value = "活动规则")
    @ApiModelProperty("活动规则")
    private String activityRules;

    /**
     * 活动奖励
     */
    @ExcelProperty(value = "活动奖励")
    @ApiModelProperty("活动奖励")
    private String activityRewards;

    /**
     * 活动状态 0正常 1停用
     */
    @ExcelProperty(value = "活动状态 0正常 1停用")
    @ApiModelProperty("活动状态 0正常 1停用")
    private Integer status;

    @ApiModelProperty("打卡次数")
    private Long clockCount = 0l;

    public List<AgentVo> loadAgentList(){
        if (ObjectUtil.isNotNull(this.activityId)){
            IActivityAgentService activityAgentService = SpringUtils.getBean("activityAgentServiceImpl", IActivityAgentService.class);
            List<AgentVo> agentVoList = activityAgentService.queryAgentList(this.activityId);
            if (ObjectUtil.isNotEmpty(agentVoList)){
                this.agentList = agentVoList;
                this.agentName = agentList.stream()
                    .map(AgentVo::getAgentName)
                    .collect(Collectors.joining(","));
            }
        }
        return this.agentList;
    }

    public List<ProductVo> loadProductList(){
        if (ObjectUtil.isNotNull(this.activityId)){
            IActivityProductService activityProductService = SpringUtils.getBean("activityProductServiceImpl", IActivityProductService.class);
            List<ProductVo> productVoList = activityProductService.selectProductList(this.activityId);
            if (ObjectUtil.isNotEmpty(productVoList)){
                this.productList = productVoList;
                this.productName = productList.stream()
                    .map(ProductVo::getProductName)
                    .collect(Collectors.joining(","));
            }
        }
        return this.productList;
    }

    public void loadImage(){
        if (ObjectUtil.isNotNull(this.image)){
            if (ObjectUtil.isNotEmpty(image)){
                this.imageUrl = image.stream()
                    .collect(Collectors.joining(","));
            }
        }
    }

    public Long loadClockCount(Employee employee){
        if (ObjectUtil.isNotNull(employee)){
            Long userId = employee.getEmployeeId();
            if (ObjectUtil.isNotEmpty(userId)){
                IClockRecordService clockRecordService = SpringUtils.getBean("clockRecordServiceImpl", IClockRecordService.class);
                this.clockCount = clockRecordService.queryClockCount(this.activityId,userId);
            }
        }
        return this.clockCount;
    }

}
