package com.hz.pm.api.workbench.model.vo;

import com.hz.pm.api.projectlib.model.vo.ProjectLibListItemVO;
import com.hz.pm.api.sys.model.vo.NoticeListItemVO;
import com.hz.pm.api.sys.model.vo.ProjectEarlyWarningVO;
import com.hz.pm.api.sys.model.vo.WflowEarlyWarningRecordsVO;
import com.hz.pm.api.todocenter.model.vo.TodoCenterStatisticsVO;
import com.ningdatech.basic.model.PageVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname WorkbenchVO
 * @Description
 * @Date 2023/3/20 9:33
 * @Author PoffyZhang
 */
@Data
public class WorkbenchVO implements Serializable {

    @ApiModelProperty("待办中心统计数据 以及代办处理")
    public TodoCenterStatisticsVO todoCerter;

    @ApiModelProperty("单位申报管理统计数据")
    public DeclaredStatistics orgDeclared;

    @ApiModelProperty("区域申报管理统计数据 只有区域管理员才有数据")
    public DeclaredStatistics regionDeclared;

    @ApiModelProperty("项目动态")
    public List<ProjectLibListItemVO> projects;

    @ApiModelProperty("公告列表")
    public Map<String,List<NoticeListItemVO>> noticeList;

    @ApiModelProperty("预警记录")
    public List<EarlyWarning> earlyWarning;

    @ApiModelProperty("项目预警记录")
    public ProjectEarlyWarning projectEarlyWarning;

    @Data
    public static class ProjectEarlyWarning {
        @ApiModelProperty("统计数据-累积")
        private WarningStatistics accumulate;

        @ApiModelProperty("统计数据-当前")
        private WarningStatistics current;

        @ApiModelProperty("超期项目预警")
        private PageVo<ProjectEarlyWarningVO> overs;

        @ApiModelProperty("临期项目预警")
        private PageVo<ProjectEarlyWarningVO> advents;

        @ApiModelProperty("正常项目")
        private PageVo<ProjectEarlyWarningVO> normals;
    }

    @Data
    public static class WarningStatistics {
        @ApiModelProperty("正常")
        private Integer normal = 0;

        @ApiModelProperty("临期")
        private Integer advent = 0;

        @ApiModelProperty("超期")
        private Integer over = 0;
    }

    @Data
    public static class EarlyWarning {
        @ApiModelProperty("类型")
        private Integer type;

        @ApiModelProperty("类型名称")
        private String typeName;

        @ApiModelProperty("总数")
        private Integer total;

        @ApiModelProperty("预警记录")
        private List<WflowEarlyWarningRecordsVO> records;
    }

    @Data
    public static class DeclaredStatistics {
        @ApiModelProperty("总项目数")
        private Integer totalNum = 0;
        @ApiModelProperty("建设项目数")
        private Integer buildNum = 0;
        @ApiModelProperty("运维项目数")
        private Integer operationNum = 0;

        @ApiModelProperty("待立项数")
        private Integer notApprovedNum = 0;
        @ApiModelProperty("已立项数")
        private Integer approvedNum = 0;
        @ApiModelProperty("建设中数")
        private Integer constructionNum = 0;
        @ApiModelProperty("待验收数")
        private Integer tobeInspectedNum = 0;
        @ApiModelProperty("已归档数")
        private Integer archivedNum = 0;

        @ApiModelProperty("申报总金额")
        private BigDecimal declaredAmount = BigDecimal.ZERO;
        @ApiModelProperty("立项总金额")
        private BigDecimal approvalAmount = BigDecimal.ZERO;


        @ApiModelProperty("申报总金额-去年")
        private BigDecimal declaredAmountLastYear = BigDecimal.ZERO;
        @ApiModelProperty("立项总金额-去年")
        private BigDecimal approvalAmountLastYear = BigDecimal.ZERO;
        @ApiModelProperty("申报总金额-较去年 增长")
        private BigDecimal declaredAmountGrowthRate = BigDecimal.ZERO;
        @ApiModelProperty("立项总金额-较去年 增长")
        private BigDecimal approvalAmountGrowthRate = BigDecimal.ZERO;

        public BigDecimal getDeclaredAmountGrowthRate(){
            if(Objects.isNull(declaredAmount)){
                this.declaredAmount = BigDecimal.ZERO;
            }

//            if((Objects.nonNull(declaredAmount)&&declaredAmount.compareTo(BigDecimal.ZERO) > 0) && (Objects.isNull(declaredAmountLastYear) ||
//                    declaredAmountLastYear.compareTo(BigDecimal.ZERO) == 0)){
//                return BigDecimal.valueOf(100);
//            }

            if(Objects.nonNull(declaredAmountLastYear) && declaredAmountLastYear.compareTo(BigDecimal.ZERO) > 0){
                return declaredAmount.subtract(declaredAmountLastYear).multiply(BigDecimal.valueOf(100))
                        .divide(declaredAmountLastYear,BigDecimal.ROUND_CEILING)
                        .setScale(BigDecimal.ROUND_CEILING, RoundingMode.CEILING);
            }
            return BigDecimal.ZERO;
        }

        public BigDecimal getApprovalAmountGrowthRate(){
            if(Objects.isNull(approvalAmount)){
                this.approvalAmount = BigDecimal.ZERO;
            }
//            if((Objects.nonNull(approvalAmount)&&approvalAmount.compareTo(BigDecimal.ZERO) > 0) && (Objects.isNull(approvalAmountLastYear) ||
//                    approvalAmountLastYear.compareTo(BigDecimal.ZERO) == 0)){
//                return BigDecimal.valueOf(100);
//            }

            if(Objects.nonNull(declaredAmountLastYear) && approvalAmountLastYear.compareTo(BigDecimal.ZERO) > 0){
                return approvalAmount.subtract(approvalAmountLastYear).multiply(BigDecimal.valueOf(100))
                        .divide(approvalAmountLastYear,BigDecimal.ROUND_CEILING)
                        .setScale(BigDecimal.ROUND_CEILING, RoundingMode.CEILING);
            }
            return BigDecimal.ZERO;
        }
    }
}
