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

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.service.EnterpriseService;
import com.gic.haoban.manage.api.dto.OpenStaffListDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.web.vo.open.OpenStaffExcelVO;
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.util.*;
import java.util.stream.Collectors;

@Service
public class OpenStaffExcelHandler extends DownloadHandlerAbstract<OpenStaffExcelVO> {

    @Autowired
    private OpenStaffApiService openStaffApiService ;
    @Autowired
    private EnterpriseService enterpriseService ;

    @Override
    public List<OpenStaffExcelVO> getData(Context context, String json, Integer currentPage) {
        OpenStaffPageQDTO qdto = JSON.parseObject(json, OpenStaffPageQDTO.class);
        BasePageInfo pageInfo = new BasePageInfo() ;
        pageInfo.setPageSize(this.getPageSize());
        pageInfo.setPageNum(currentPage);
        ServiceResponse<Page<OpenStaffListDTO>> page = this.openStaffApiService.page(qdto, pageInfo);
        List<OpenStaffExcelVO> voList = new ArrayList<>();
        if (null != page && CollectionUtils.isNotEmpty(page.getResult().getResult())) {
            EnterpriseDTO en = this.enterpriseService.getEnterpriseById(qdto.getEnterpriseId()) ;
            for(OpenStaffListDTO item : page.getResult().getResult()) {
                OpenStaffExcelVO vo = EntityUtil.changeEntityByJSON(OpenStaffExcelVO.class,item) ;
                vo.setCreateTimeExcel(DateUtil.dateToStr(item.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
                if(null != item.getAuthTime()) {
                    vo.setLastTimeExcel(DateUtil.dateToStr(item.getAuthTime(),"yyyy-MM-dd HH:mm:ss"));
                }
                if(CollectionUtils.isNotEmpty(item.getClerkList())) {
                    vo.setStoreCodes(item.getClerkList().stream().map(o->o.getStoreCode()).collect(Collectors.joining(",")));
                    vo.setStoreNames(item.getClerkList().stream().map(o->o.getStoreName()).collect(Collectors.joining(",")));
                    vo.setClerkCodes(item.getClerkList().stream().map(o->o.getClerkCode()).collect(Collectors.joining(",")));
                    vo.setClerkNames(item.getClerkList().stream().map(o->o.getClerkName()).collect(Collectors.joining(",")));
                }
                if(item.getStatusFlag()==1) {
                    vo.setStatusExcel("托管中");
                }else {
                   if(item.getValidQrcodeFlag()==1) {
                       vo.setStatusExcel("待验证");
                   }else {
                       if (null == item.getFirstAuthTime()) {
                           vo.setStatusExcel("待授权");
                       }else{
                           vo.setStatusExcel("已过期");
                       }
                   }
                }
                vo.setBrandName(en.getBrandName());
                voList.add(vo) ;
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList("成员姓名", "托管状态","关联导购姓名","关联导购code","关联门店姓名","关联门店code","所属商户","最近托管授权时间","新增时间");
    }

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

    @Override
    public List<String> getColumns() {
        return Arrays.asList("staffName",  "statusExcel", "clerkNames","clerkCodes", "storeNames","storeCodes","brandName","lastTimeExcel","createTimeExcel");
    }

    @Override
    public Integer getPageSize() {
        return 1000 ;
    }
}
