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.haoban.manage.web.vo.open.AddMemberExcelVO;
import com.gic.haoban.task.manage.api.dto.open.OpenQwAddTaskMemberDTO;
import com.gic.haoban.task.manage.api.qdto.open.AddTaskMemberPageQDTO;
import com.gic.haoban.task.manage.api.service.OpenQwApiService;
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.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class OpenAddMemberExcelHandler extends DownloadHandlerAbstract<AddMemberExcelVO> {

    @Autowired
    private OpenQwApiService openQwApiService;

    @Override
    public List<AddMemberExcelVO> getData(Context context, String json, Integer currentPage) {
        AddTaskMemberPageQDTO qdto = JSON.parseObject(json, AddTaskMemberPageQDTO.class);
        BasePageInfo pageInfo = new BasePageInfo();
        pageInfo.setPageSize(this.getPageSize());
        pageInfo.setPageNum(currentPage);
        ServiceResponse<Page<OpenQwAddTaskMemberDTO>> page = this.openQwApiService.addFriendPage(qdto, pageInfo);
        List<AddMemberExcelVO> voList = new ArrayList<>();
        if (null != page && CollectionUtils.isNotEmpty(page.getResult().getResult())) {
            for (OpenQwAddTaskMemberDTO item : page.getResult().getResult()) {
                AddMemberExcelVO vo = EntityUtil.changeEntityByJSON(AddMemberExcelVO.class, item);
                if (item.getStatusFlag() == 1) {
                    vo.setStatusExcel("待通过");
                } else {
                    vo.setStatusExcel("已添加");
                }
                if (null != item.getApplyTime()) {
                    vo.setApplyTimeExcel(DateUtil.dateToStr(item.getApplyTime(), "yyyy-MM-dd HH:mm:ss"));
                }
                if (null != item.getAddTime()) {
                    vo.setAddTimeExcel(DateUtil.dateToStr(item.getAddTime(), "yyyy-MM-dd HH:mm:ss"));
                }
                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", "clerkName", "clerkCode", "storeName", "storeCode", "memberName", "memberNick", "memberPhone", "statusExcel", "addTimeExcel", "applyTimeExcel");
    }

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