package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityHundredAgentExportDto;
import com.crm.manager.service.activity.CustomActivityHundredAgentVipFeignService;
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.hundred.agent.CustomActivityHundredAgentDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.agent.CustomActivityHundredAgentApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.agent.CustomActivityHundredAgentDeleteEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.agent.CustomActivityHundredAgentSearchEntity;
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/hundred/agent/vip")
public class CustomActivityHundredAgentVipController {

    @Autowired
    private CustomActivityHundredAgentVipFeignService service;


    /**
     * 销售审核
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/sales/approve")
    public BaseResultDto salesApprove(@RequestBody @Validated CustomActivityHundredAgentApproveEntity entity) throws
            Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity
                .getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        return service.salesApprove(entity);
    }

    @PostMapping("/realization/approve")
    public BaseResultDto realizationApprove(@RequestBody @Validated CustomActivityHundredAgentApproveEntity 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 CustomActivityHundredAgentDeleteEntity entity) throws Exception {

        return service.delete(entity);
    }


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

        return service.searcherList(entity);

    }

    @PostMapping("/search/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        return service.searcherSingle(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 = "realizationStatus") Integer realizationStatus,
                       @RequestParam(name = "startDate") String startDate,
                       @RequestParam(name = "endDate") String endDate,
                       HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityHundredAgentSearchEntity entity = new CustomActivityHundredAgentSearchEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setEmail(email);
        entity.setStatus(status);
        entity.setRealizationStatus(realizationStatus);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

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

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityHundredAgentExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityHundredAgentExportDto dto = new CustomActivityHundredAgentExportDto();
                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.getRealizationStatus() == null || item.getRealizationStatus().equals(0)) {
                    dto.setRealizationStatus("未申请");
                } else if (item.getRealizationStatus().equals(1)) {
                    dto.setRealizationStatus("申请中");
                } else if (item.getRealizationStatus().equals(2)) {
                    dto.setRealizationStatus("通过");
                } else if (item.getRealizationStatus().equals(3)) {
                    dto.setRealizationStatus("拒绝");
                }
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_ACTIVITY_HUNDRED_AGENT", true), dtos,
                    CustomActivityHundredAgentExportDto.class, response);
        }
    }

}
