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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.EnterpriseDTO;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanAttendDTO;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanClerkListQDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanDataListQDTO;
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.MomentClerkExcelVO;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MomentClerkExcelHandler extends DownloadHandlerAbstract<MomentClerkExcelVO> {

    @Autowired
    private QwMomentApiService qwMomentApiService ;
    @Autowired
    private StoreService storeService  ;

    @Override
    public List<MomentClerkExcelVO> getData(Context context, String json, Integer currentPage) {
        PlanClerkListQDTO qdto = JSON.parseObject(json, PlanClerkListQDTO.class);
        BasePageInfo pageInfo = new BasePageInfo() ;
        pageInfo.setPageSize(this.getPageSize());
        pageInfo.setPageNum(currentPage);
        ServiceResponse<QwMomentPlanDTO> detaiResp = this.qwMomentApiService.detail(qdto.getPlanId()) ;
        boolean isEnd = detaiResp.getResult().getStatusFlag()==2 || detaiResp.getResult().getEndDate().getTime()<System.currentTimeMillis() ;
        ServiceResponse<Page<QwMomentPlanAttendDTO>> pageResp = this.qwMomentApiService.attendList(qdto);
        List<MomentClerkExcelVO> voList = new ArrayList<>();
        if (null != pageResp && CollectionUtils.isNotEmpty(pageResp.getResult().getResult())) {
            for(QwMomentPlanAttendDTO item : pageResp.getResult().getResult()) {
                MomentClerkExcelVO vo = EntityUtil.changeEntityByJSON(MomentClerkExcelVO.class,item) ;
                if(StringUtils.isNotBlank(vo.getStoreId())) {
                    StoreDTO store = this.storeService.getStore(vo.getStoreId());
                    vo.setStoreName(store.getStoreName());
                    vo.setStoreCode(store.getStoreCode());
                }
                Date sendTime = vo.getSendTime() ;
                if(null != sendTime) {
                    vo.setSendTimeExcel(DateUtil.dateToStr(sendTime,"yyyy-MM-dd HH:mm:ss"));
                }
                int taskStatus = vo.getTaskStatus();
                String taskStatusExcel = "未完成" ;
                if(taskStatus==2) {
                    taskStatusExcel = "已完成" ;
                }else {
                    if(isEnd) {
                        taskStatusExcel = "已失效" ;
                    }
                }
                vo.setTaskStatusExcel(taskStatusExcel);
                voList.add(vo) ;
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList();
    }

    @Override
    public List<String> getColumnNames(Context context) {
        String json = context.getDownloadReportDTO().getSearchDataParams();
        PlanClerkListQDTO qdto = JSON.parseObject(json, PlanClerkListQDTO.class);
        if(qdto.getStatusFlag()==1) {
            return Arrays.asList("员工姓名", "员工id" ,  "导购名称", "导购code","所属门店名称", "门店code","完成情况","发布时间","点赞数","评论数");
        }else {
            return Arrays.asList("员工姓名", "员工id" ,  "导购名称", "导购code","所属门店名称", "门店code","下发失败原因");
        }
    }

    @Override
    public List<String> getColumns(Context context) {
        String json = context.getDownloadReportDTO().getSearchDataParams();
        PlanClerkListQDTO qdto = JSON.parseObject(json, PlanClerkListQDTO.class);
        if(qdto.getStatusFlag()==1) {
            return Arrays.asList("staffName", "staffId", "clerkName", "clerkCode","storeName", "storeCode","taskStatusExcel","sendTimeExcel","likeCount","commentCount");
        }else {
            return Arrays.asList("staffName", "staffId", "clerkName", "clerkCode","storeName", "storeCode","reason");
        }
    }

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

    @Override
    public List<String> getColumns() {
        return Arrays.asList();
    }
}
