package com.crm.manager.controller;

import com.crm.manager.dto.export.FollowDealExportDto;
import com.crm.manager.service.follow.FollowDealFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.follow.FollowDealSearchDto;
import com.crm.rely.backend.core.dto.follow.daily.FollowDealScoreDailyDto;
import com.crm.rely.backend.core.dto.follow.score.FollowDealScorePeriodTypesDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.follow.deal.*;
import com.crm.rely.backend.util.DateUtil;
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;

@RestController
@RequestMapping("/follow/deal")
public class FollowDealController {

    @Autowired
    private FollowDealFeignService service;

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated FollowDealAddEntity entity) throws Exception {

        return service.managerAdd(entity);
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated FollowDealDeleteEntity entity) throws Exception {

        return service.delete(entity);
    }

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

        return service.approve(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated FollowDealUpdateEntity entity) throws Exception {
        return service.managerUpdate(entity);
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public ResultWithPagerDto<FollowDealSearchDto> pageSearchList(@RequestBody @Validated FollowDealSearchListEntity entity) throws Exception {

        return service.pageSearchList(entity);
    }

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

        return service.single(entity);
    }

    @PostMapping("/clear/all/recommend")
    public BaseResultDto clearAllRecommend() throws Exception {

        return service.clearAllRecommend();
    }

    @PostMapping("/recommend/update")
    public BaseResultDto managerRecommendUpdate(@RequestBody @Validated FollowDealRecommendUpdateEntity entity) throws Exception {

        return service.recommendUpdate(entity);
    }


    @RequestMapping("/export")
    public void export(@RequestParam(name = "platform") String platform, @RequestParam(name = "login") Long login, @RequestParam(name = "status") Integer status, @RequestParam(name = "pIbNo") String pIbNo, @RequestParam(name = "salesNo") String salesNo, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FollowDealSearchListEntity entity = new FollowDealSearchListEntity();
        entity.setPlatform(platform);
        entity.setLogin(login);
        entity.setStatus(status);
        entity.setPIbNo(pIbNo);
        entity.setSalesNo(salesNo);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));

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

        if (resultDto != null && resultDto.getCode() == 200) {
            List<FollowDealExportDto> dtos = Lists.newArrayList();
            if (resultDto.getData() != null) {
                resultDto.getData().forEach((item) -> {
                    FollowDealExportDto dto = new FollowDealExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setPIBNO(item.getPIbNo());
                    dto.setSalesNo(item.getSalesNo());
                    dto.setDealCId(String.valueOf(item.getDealCId()));
                    dto.setDealCId(String.valueOf(item.getDealCId()));
                    switch (item.getDealLoginType()) {
                        case 1:
                            dto.setDealLoginType("经典账户");
                            break;
                        case 2:
                            dto.setDealLoginType("ECN");
                            break;
                        case 3:
                            dto.setDealLoginType("机构账户");
                            break;
                        case 5:
                        case 6:
                            dto.setDealLoginType("极速账户");
                            break;
                    }
                    if (item.getPermissionDisplay() != null) {
                        if (item.getPermissionDisplay() == 1) {
                            dto.setPermissionDisplay("平台");
                        } else {
                            dto.setPermissionDisplay("代理");
                        }
                    }
                    dto.setWalletAmount(item.getWalletAmount().toPlainString());
                    dto.setFrozenAmount(item.getFrozenAmount().toPlainString());
                    dto.setAddTime(DateUtil.formatTime(item.getAddTime()));

                    dtos.add(dto);
                });
            }

            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("FOLLOW_DEAL", true), dtos, FollowDealExportDto.class, response);
        }
    }

    /**
     * 信号源排名周期
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/search/period")
    public BaseResultDto<List<FollowDealScorePeriodTypesDto>> searchPeriod() throws Exception {
        return service.managerSearchPeriod();
    }

    @PostMapping("/search/periodRanking")
    public BaseResultWithPagerDto<List<FollowDealScoreDailyDto>> searchPeriodRanking(@RequestBody FollowDealSearchListEntity entity) throws Exception {
        return service.searchPeriodRanking(entity);
    }
}
