package com.gic.haoban.manage.web.controller.moment;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.qdto.moment.QwMomentPlanQDTO;
import com.gic.haoban.manage.api.service.moment.QwMomentApiService;
import com.gic.haoban.manage.web.vo.moment.MomentPlanExcelVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class MomentPlanExcelHandler  extends DownloadHandlerAbstract<MomentPlanExcelVO> {

    @Autowired
    private QwMomentApiService qwMomentApiService ;
    @Override
    public List<MomentPlanExcelVO> getData(Context context, String json, Integer currentPage) {
        QwMomentPlanQDTO qdto = JSON.parseObject(json, QwMomentPlanQDTO.class);
        qdto.setPageSize(this.getPageSize());
        qdto.setPageNum(currentPage);
        ServiceResponse<Page<QwMomentPlanDTO>> pageResp = this.qwMomentApiService.planList(qdto);
        List<MomentPlanExcelVO> voList = new ArrayList<>();
        if (null != pageResp && CollectionUtils.isNotEmpty(pageResp.getResult().getResult())) {
            for(QwMomentPlanDTO item : pageResp.getResult().getResult()) {
                MomentPlanExcelVO vo = EntityUtil.changeEntityByJSON(MomentPlanExcelVO.class,item) ;
                int execType = vo.getExecType() ;
                String execTypeExcel = "企业快捷发布" ;
                if(execType==1) {
                    execTypeExcel = "好办小程序发布" ;
                }
                vo.setExecTypeExcel(execTypeExcel);
                int statusFlag = vo.getStatusFlag() ;
                String statusFlagExcel = "进行中" ;
                if(statusFlag==2) {
                    statusFlagExcel = "已终止" ;
                }else  {
                    if (vo.getBeginDate().getTime() > System.currentTimeMillis()) {
                        statusFlagExcel = "未开始" ;
                    }else if(vo.getEndDate().getTime() < System.currentTimeMillis()) {
                        statusFlagExcel = "已结束" ;
                    }
                }
                if(null != vo.getExecRate() && vo.getExecRate() > 0) {
                    vo.setExecRateFloat(BigDecimal.valueOf(vo.getExecRate()).divide(BigDecimal.valueOf(100),2,2).doubleValue());
                }
                vo.setStatusFlagExcel(statusFlagExcel);
                voList.add(vo) ;
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList("活动信息", "执行方式","活动状态","下发任务数","完成任务数","完成率","点赞数","评论数");
    }

    @Override
    public LinkedHashMap<String, List<String>> doubleColumnNames() {
        return null;
    }

    @Override
    public List<String> getColumns() {
        return Arrays.asList("title",  "execTypeExcel", "statusFlagExcel","taskClerkNum", "doneNum","execRateFloat","totalLikeCount","totalCommentCount");
    }
}
