package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityRequiteApplyExportDto;
import com.crm.manager.service.activity.CustomActivityRequiteApplyFeignService;
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.custom.activity.requite.CustomActivityRequiteApplyDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.requite.CustomActivityRequiteApplyApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.requite.CustomActivityRequiteApplyDeleteEntity;
import com.crm.rely.backend.core.entity.custom.activity.requite.CustomActivityRequiteApplySearchEntity;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Log4j
@RestController
@RequestMapping("/custom/activity/requite/apply")
public class CustomActivityRequiteApplyController {

    @Autowired
    private CustomActivityRequiteApplyFeignService feignService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomActivityRequiteApplyDto> searcherList(@RequestBody @Validated CustomActivityRequiteApplySearchEntity entity) throws Exception {

        return feignService.searcherList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return feignService.searcherSingle(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomActivityRequiteApplyDeleteEntity entity) throws Exception {

        return feignService.delete(entity);
    }

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated CustomActivityRequiteApplyApproveEntity entity) throws Exception {

        return feignService.approve(entity);
    }

    @PostMapping("/cancel")
    public BaseResultDto cancel(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return feignService.cancel(entity);
    }

    @GetMapping("/search/list/export")
    public void searcherListExport(@RequestParam(name = "startDate") String startDate,
                                   @RequestParam(name = "endDate") String endDate,
                                   @RequestParam(name = "cId") Long cId,
                                   @RequestParam(name = "login") Long login,
                                   @RequestParam(name = "status") Integer status,
                                   @RequestParam(name = "endStatus") Integer endStatus,
                                   HttpServletResponse response) throws Exception {

        CustomActivityRequiteApplySearchEntity entity = new CustomActivityRequiteApplySearchEntity();
        entity.setCId(cId);
        entity.setLogin(login);
        entity.setStatus(status);
        entity.setEndStatus(endStatus);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (!StringUtils.isEmpty(startDate)) {
            entity.setStartDate(sdf.parse(startDate));
        }
        if (!StringUtils.isEmpty(endDate)) {
            entity.setEndDate(sdf.parse(endDate));
        }

        List<CustomActivityRequiteApplyDto> resultDto = feignService.searcherListExport(entity);
        List<CustomActivityRequiteApplyExportDto> exportDtos = Lists.newArrayList();
        resultDto.forEach((item) -> {
            CustomActivityRequiteApplyExportDto exportDto = new CustomActivityRequiteApplyExportDto();
            BeanUtils.copyProperties(item, exportDto);
            exportDto.setCID(item.getCId());
            exportDto.setInitTime(DateUtil.formatTime(item.getInitTime()));
            if(1 == item.getStatus()){
                exportDto.setStatus("待处理");
            }else if(2 == item.getStatus()){
                exportDto.setStatus("已通过");
            }else{
                exportDto.setStatus("已拒绝");
            }
            if (0 == item.getEndStatus()) {
                exportDto.setEndStatus("未开始");
            } else if (1 == item.getEndStatus()) {
                exportDto.setEndStatus("活动中");
            } else if (2 == item.getEndStatus()) {
                exportDto.setEndStatus("已结束");
            } else {
                exportDto.setEndStatus("已取消");
            }
            exportDtos.add(exportDto);
        });
        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CUSTOM_ACTIVITY_REQUITE"), exportDtos, CustomActivityRequiteApplyExportDto.class, response);

    }

}
