package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityHundredExportDto;
import com.crm.manager.service.activity.CustomActivityHundredFeignService;
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.CustomActivityHundredDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.CustomActivityHundredApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.CustomActivityHundredDeleteEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.CustomActivityHundredSearchEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.comment.CustomActivityHundredCommentAddEntity;
import com.crm.rely.backend.core.entity.custom.activity.hundred.comment.CustomActivityHundredCommentSearchEntity;
import com.crm.rely.backend.exception.ServiceException;
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")
public class CustomActivityHundredController {

    @Autowired
    private CustomActivityHundredFeignService service;


    /**
     * 销售审核
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/sales/approve")
    public BaseResultDto salesApprove(@RequestBody @Validated CustomActivityHundredApproveEntity 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("/transform/approve")
    public BaseResultDto transformApprove(@RequestBody @Validated CustomActivityHundredApproveEntity entity) throws
            Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity
                .getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        return service.transformApprove(entity);
    }


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


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

        return service.searcherList(entity);

    }

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

    /**
     * 添加备注
     *
     * @param entity
     * @return
     * @throws ServiceException
     */
    @PostMapping("/comment")
    public BaseResultDto addComment(@RequestBody @Validated CustomActivityHundredCommentAddEntity entity) throws
            ServiceException {

        return service.addComment(entity);
    }

    /**
     * 查看备注
     *
     * @param entity
     * @return
     * @throws ServiceException
     */
    @PostMapping("/search/list/comment")
    public BaseResultDto searchComment(@RequestBody @Validated CustomActivityHundredCommentSearchEntity entity)
            throws ServiceException {

        return service.searchComment(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 = "realizationStatus") Integer realizationStatus,
                       @RequestParam(name = "startDate") String startDate,
                       @RequestParam(name = "endDate") String endDate,
                       HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityHundredSearchEntity entity = new CustomActivityHundredSearchEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setEmail(email);
        entity.setStatus(status);
        entity.setActivityStatus(activityStatus);
        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<CustomActivityHundredDto>> resultDto = service.export(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityHundredExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityHundredExportDto dto = new CustomActivityHundredExportDto();
                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() == null || item.getActivityStatus().equals(0)) {
                    dto.setActivityStatus("未申请");
                } else if (item.getActivityStatus().equals(1)) {
                    dto.setActivityStatus("申请中");
                } else if (item.getActivityStatus().equals(2)) {
                    dto.setActivityStatus("通过");
                } else if (item.getActivityStatus().equals(3)) {
                    dto.setActivityStatus("拒绝");
                }

                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", true), dtos,
                    CustomActivityHundredExportDto.class, response);
        }
    }

}
