package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityUcoinRewardExportDto;
import com.crm.manager.service.activity.CustomActivityUcoinRewardFeignService;
import com.crm.rely.backend.core.dto.activity.ucoin.CustomActivityUcoinRewardDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.ucoin.CustomActivityUcoinRewardSearchEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/custom/activity/ucoin")
public class CustomActivityUcoindRewardController {

    @Autowired
    private CustomActivityUcoinRewardFeignService feignService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomActivityUcoinRewardDto> searcherList(@RequestBody @Validated CustomActivityUcoinRewardSearchEntity entity) throws Exception {
        ResultWithPagerDto<CustomActivityUcoinRewardDto> resultWithPagerDto = feignService.pageList(entity);
        return resultWithPagerDto;
    }

    /**
     * 完成任务
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/complete")
    public BaseResultDto complete(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return feignService.complete(entity);
    }

    /**
     * 取消
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/cancel")
    public BaseResultDto cancel(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return feignService.cancel(entity);
    }

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

        CustomActivityUcoinRewardSearchEntity entity = new CustomActivityUcoinRewardSearchEntity();
        entity.setCId(cId);
        entity.setLogin(login);
        entity.setStatus(status);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (StringUtils.isEmpty(startDate)) {
            entity.setStartDate(DateUtil.getMonthStart(new Date()));
        } else {
            entity.setStartDate(sdf.parse(startDate));
        }
        if (StringUtils.isEmpty(endDate)) {
            entity.setEndDate(DateUtil.addDate(new Date(), 1));
        } else {
            entity.setEndDate(sdf.parse(endDate));
        }*/

        List<CustomActivityUcoinRewardDto> resultDto = feignService.searcherListExport(entity);
        List<CustomActivityUcoinRewardExportDto> exportDtos = Lists.newArrayList();
        resultDto.forEach((item) -> {
            CustomActivityUcoinRewardExportDto exportDto = new CustomActivityUcoinRewardExportDto();
            BeanUtils.copyProperties(item, exportDto);
            exportDto.setCID(item.getCId());
            if(1 == item.getStatus()){
                exportDto.setStatus("任务中");
            }else if(2 == item.getStatus()){
                exportDto.setStatus("完成任务");
            }else if(3 == item.getStatus()){
                exportDto.setStatus("已取消");
            }else{
                exportDto.setStatus("已结束");
            }
            exportDtos.add(exportDto);
        });
        ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("CUSTOM_ACTIVITY_UCOIN_REWARD"), exportDtos, CustomActivityUcoinRewardExportDto.class, response);

    }

}
