package com.crm.manager.controller;

import com.crm.manager.dto.export.CardOperateRecordExportDto;
import com.crm.manager.service.ucard.UcardOperateFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.CardOperateDto;
import com.crm.rely.backend.core.entity.ucard.CardOperatePageEntity;
import com.crm.rely.backend.core.enums.CardOperateTypeEnum;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.crm.rely.backend.util.NameUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作记录服务接口
 */
@Slf4j
@RestController
@RequestMapping("/ucard/operate")
public class UcardOperateController {

    @Autowired
    private UcardOperateFeignService ucardOperateFeignService;

    /**
     * 操作记录分页列表
     */
    @PostMapping("/page")
    public ResultWithPagerDto<CardOperateDto> queryOperatePageList(@RequestBody CardOperatePageEntity entity) throws ServiceException {
        return ucardOperateFeignService.queryOperatePageList(entity);
    }

    /**
     * 操作记录列表导出
     */
    @RequestMapping("/record/export")
    public void cardOperateRecordExport(@RequestBody CardOperatePageEntity entity, HttpServletResponse response) throws Exception {
        BaseResultDto<List<CardOperateDto>> resultDto = ucardOperateFeignService.cardOperateRecordExport(entity);
        if (resultDto != null && resultDto.getCode() == 200) {
            List<CardOperateRecordExportDto> dtos = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(resultDto.getData())) {
                resultDto.getData().forEach((item) -> {
                    CardOperateRecordExportDto dto = new CardOperateRecordExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dto.setName(NameUtil.getName(item.getCountry(), item.getLastName(), null, item.getFirstName()));
                    item.setAreaCode(item.getAreaCode() == null ? "" : item.getAreaCode());
                    item.setMobile(item.getMobile() == null ? "" : item.getMobile());
                    dto.setMobile(item.getAreaCode() + " " + item.getMobile());
                    dto.setOperateType(CardOperateTypeEnum.matchCode(item.getOperateType()));
                    dtos.add(dto);
                });
            }
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CARD_OPERATE_RECORD"), dtos, CardOperateRecordExportDto.class, response);
        }
    }

}
