package com.crm.manager.controller;

import com.crm.manager.dto.export.FollowDealSubscribeExportDto;
import com.crm.manager.service.follow.FollowDealSubscribeFeignService;
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.follow.deal.subscribe.FollowDealSubscribeSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.follow.deal.subscribe.FollowDealSubscribeAddEntity;
import com.crm.rely.backend.core.entity.follow.deal.subscribe.FollowDealSubscribeDeleteEntity;
import com.crm.rely.backend.core.entity.follow.deal.subscribe.FollowDealSubscribeSearchListEntity;
import com.crm.rely.backend.core.entity.follow.deal.subscribe.FollowDealSubscribeUpdateEntity;
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/subscribe")
public class FollowDealSubscribeController {

    @Autowired
    private FollowDealSubscribeFeignService service;

    /**
     * 订阅
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/subscribe")
    public BaseResultDto subscribe(@RequestBody @Validated FollowDealSubscribeAddEntity entity) throws Exception {

        return service.subscribe(entity);
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated FollowDealSubscribeDeleteEntity entity) throws Exception {
        return service.delete(entity);
    }

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

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

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

        return service.pageSearchList(entity);
    }

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

    @RequestMapping("/export")
    public void export(@RequestParam(name = "dealPlatform") String dealPlatform,
                       @RequestParam(name = "followPlatform") String followPlatform,
                       @RequestParam(name = "dealLogin") Long dealLogin,
                       @RequestParam(name = "followLogin") Long followLogin,
                       @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");
        FollowDealSubscribeSearchListEntity entity = new FollowDealSubscribeSearchListEntity();
        entity.setDealPlatform(dealPlatform);
        entity.setFollowPlatform(followPlatform);
        entity.setDealLogin(dealLogin);
        entity.setFollowLogin(followLogin);
        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<FollowDealSubscribeSearchDto>> resultDto = service.export(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<FollowDealSubscribeExportDto> dtos = Lists.newArrayList();

            if (resultDto.getData() != null) {
                resultDto.getData().forEach((item) -> {
                    FollowDealSubscribeExportDto dto = new FollowDealSubscribeExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setDealLogin(item.getDealLogin());
                    dto.setFollowLogin(item.getFollowLogin());
                    dto.setPIBNO(item.getPIbNo());
                    dto.setAgentName(item.getAgentName());
                    dto.setSalesNo(item.getSalesNo());
                    switch (item.getFollowType()) {
                        case 1:
                            dto.setFollowLoginType("经典账户");
                            break;
                        case 2:
                            dto.setFollowLoginType("ECN");
                            break;
                        case 3:
                            dto.setFollowLoginType("机构账户");
                            break;
                        case 5:
                        case 6:
                            dto.setFollowLoginType("极速账户");
                            break;
                    }

                    dto.setFollowProfit(String.format("%.2f", item.getFollowProfit()));
                    dto.setStartTime(DateUtil.formatTime(item.getStartTime()));

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

}
