package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityNoWorriesExportDto;
import com.crm.manager.service.activity.CustomActivityNoWorriesFeignService;
import com.crm.rely.backend.core.constant.Constants;
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.no.worries.CustomActivityNoWorriesDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.no.worries.CustomActivityNoWorriesApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.no.worries.CustomActivityNoWorriesDeleteEntity;
import com.crm.rely.backend.core.entity.custom.activity.no.worries.CustomActivityNoWorriesSearchEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
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.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/custom/activity/no/worries")
public class CustomActivityNoWorriesController {

    @Autowired
    private CustomActivityNoWorriesFeignService service;


    @PostMapping("/realization/approve")
    public BaseResultDto realizationApprove(@RequestBody @Validated CustomActivityNoWorriesApproveEntity entity) throws
            Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity
                .getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        return service.realizationApprove(entity);
    }


    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomActivityNoWorriesDeleteEntity entity) throws Exception {
        service.delete(entity);
        return BaseResultDto.success();
    }


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

        return service.searcherList(entity);

    }

    @PostMapping("/search/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return service.searcherSingle(entity);
    }

    @PostMapping("/cancel")
    public BaseResultDto cancel(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return service.cancel(entity);
    }


    @RequestMapping("/export")
    public void export(@RequestParam(name = "login") Long login,
                       @RequestParam(name = "cId") Long cId,
                       @RequestParam(name = "email") String email,
                       @RequestParam(name = "status") Integer status,
                       @RequestParam(name = "activityStatus") Integer activityStatus,
                       @RequestParam(name = "startDate") String startDate,
                       @RequestParam(name = "endDate") String endDate,
                       HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityNoWorriesSearchEntity entity = new CustomActivityNoWorriesSearchEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setEmail(email);
        entity.setStatus(status);
        entity.setActivityStatus(activityStatus);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

        BaseResultDto<List<CustomActivityNoWorriesDto>> resultDto = service.export(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityNoWorriesExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityNoWorriesExportDto dto = new CustomActivityNoWorriesExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

                if (item.getStatus() == null || item.getStatus().equals(0)) {
                    dto.setStatus("未申请");
                } else if (item.getStatus().equals(1)) {
                    dto.setStatus("申请中");
                } else if (item.getStatus().equals(2)) {
                    dto.setStatus("通过");
                } else if (item.getStatus().equals(3)) {
                    dto.setStatus("拒绝");
                }
                if (item.getActivityStatus().equals(1)) {
                    dto.setActivityStatus("活动中");
                } else if (item.getActivityStatus().equals(2)) {
                    dto.setActivityStatus("活动完成");
                } else if (item.getActivityStatus().equals(3)) {
                    dto.setActivityStatus("活动取消");
                } else if (item.getActivityStatus().equals(5)) {
                    dto.setActivityStatus("活动完成");
                }

                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_ACTIVITY_NO_WORRIES", true), dtos,
                    CustomActivityNoWorriesExportDto.class, response);
        }
    }

}
