package com.crm.manager.controller;

import com.crm.manager.service.SalesPerformanceConfigService;
import com.crm.manager.service.SalesPerformanceInfoService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.sales.SalesPerformanceConfigDto;
import com.crm.rely.backend.core.dto.sales.SalesPerformanceInfoDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigSearchEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceConfigUpdateEntity;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceInfoSearchEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.crm.rely.backend.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sales/performance/info")
public class SalesPerformanceInfoController {

    @Value("${web.export-path}")
    private String exportPath;

    @Autowired
    private SalesPerformanceInfoService salesPerformanceInfoService;

    @PostMapping("/search/single")
    public BaseResultDto<SalesPerformanceConfigDto> searchSingle(@RequestBody @Validated SingleLongEntity entity, InfoEntity infoEntity) throws ServiceException {
        return BaseResultDto.success(salesPerformanceInfoService.searchById(entity.getId(), infoEntity));
    }

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<SalesPerformanceInfoDto>> searchList(@RequestBody SalesPerformanceInfoSearchEntity entity, InfoEntity infoEntity) throws ServiceException {
        prepareSearchListEntity(entity, infoEntity);
        return salesPerformanceInfoService.searchList(entity);
    }

    @GetMapping("/export")
    public void export(@RequestParam(name = "salesType") Integer salesType, @RequestParam(name = "salesName") String salesName, @RequestParam(name = "saleNo") String saleNo, @RequestParam(name = "ym") String ym, HttpServletResponse response, InfoEntity infoEntity) throws ServiceException {
        SalesPerformanceInfoSearchEntity entity = new SalesPerformanceInfoSearchEntity();
        entity.setSalesType(salesType);
        entity.setSalesName(salesName);
        entity.setSaleNo(saleNo);
        entity.setYm(ym);
        prepareSearchListEntity(entity, infoEntity);
        entity.setPage(null);
        String fileName = salesPerformanceInfoService.export(entity);
        try {
            ExportUtil.transferToResponse(fileName, exportPath, response);
            FileProcessUtil.deleteFiles(Arrays.asList(exportPath + File.separator + fileName));
        } catch (Exception e) {
            throw ServiceException.exception(e.getMessage());
        }


    }

    private void prepareSearchListEntity(SalesPerformanceInfoSearchEntity entity, InfoEntity infoEntity) {
        if (StringUtils.isEmpty(entity.getYm())) {
            Date now = new Date();
            entity.setYm(DateUtil.format("yyyyMM", now));
        }
        entity.setReportDate(DateUtil.parse("yyyyMMdd", entity.getYm() + "01"));
        entity.setStamp(infoEntity.getIbInfo().getStamp());

    }
}
