package com.crm.manager.controller;


import com.crm.manager.dto.export.CustomAccountExportDto;
import com.crm.manager.service.CustomGroupService;
import com.crm.manager.service.DataRoleService;
import com.crm.manager.service.custom.CustomLoginFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
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.custom.account.CustomAccountDto;
import com.crm.rely.backend.core.dto.custom.group.CustomGroupStaticDto;
import com.crm.rely.backend.core.dto.custom.login.CustomLoginCloseReasonDto;
import com.crm.rely.backend.core.dto.custom.login.CustomLoginFloatSumDto;
import com.crm.rely.backend.core.dto.custom.login.operate.CustomLoginOperateRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.IbInfoEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.login.*;
import com.crm.rely.backend.core.entity.custom.login.operate.CustomLoginOperateRecordSearchEntity;
import com.crm.rely.backend.exception.ServiceException;
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.Date;
import java.util.List;

/**
 * @description: 客户交易账户控制器
 * @author: houn
 * @create: 2020-06-13 18:02
 **/
@RestController
@RequestMapping("/custom/login")
public class CustomLoginController {

    @Autowired
    private CustomGroupService customGroupService;
    @Autowired
    private CustomLoginFeignService customLoginFeignService;

    @Autowired
    private DataRoleService dataRoleService;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated CustomLoginAddEntity entity) throws Exception {

        if (entity.getCustomId() == null) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        entity.setLoginStatus(Constants.MANAGER_PENDING_STATUS);
        return customLoginFeignService.managerAdd(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomLoginDeleteEntity entity) throws Exception {

        return customLoginFeignService.managerDelete(entity);
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated CustomLoginUpdateEntity entity) throws Exception {

        entity.setLoginStatus(Constants.MANAGER_PENDING_STATUS);
        return customLoginFeignService.managerUpdate(entity);
    }

    @PostMapping("/update/reset/status")
    public BaseResultDto updateLoginStatus(@RequestBody @Validated CustomLoginUpdateRestStatusEntity entity) throws Exception {

        return customLoginFeignService.managerUpdateResetStatus(entity);
    }

    @PostMapping("/search/info")
    public CustomGroupStaticDto info() throws Exception {
        return customGroupService.getStaticData();
    }

    /**
     * 查看单个客户列表(customId不为空)
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/custom/list")
    public BaseResultDto<List<CustomAccountDto>> searcherList(@RequestBody @Validated CustomLoginSearchListByCustomEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            entity.setStamp(infoEntity.getIbInfo().getStamp());
//        }else {
//            entity.setStamp(null);
//        }
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        if (entity.getCustomId() == null && entity.getCId() == null) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }

        IbInfoEntity ibInfoEntity = infoEntity.getIbInfo();

        entity.setGroupId(ibInfoEntity.getGroupId());
        return customLoginFeignService.searchCustomList(entity);
    }

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

        return customLoginFeignService.manageSearchSingle(entity);
    }

    /**
     * 查看客户交易账户
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<CustomAccountDto>> searcherCustomList(@RequestBody @Validated CustomLoginSearchListEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            entity.setStamp(infoEntity.getIbInfo().getStamp());
//        }else {
//            entity.setStamp(null);
//        }
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        return customLoginFeignService.searchList(entity);
    }

    @PostMapping("/account/search/list")
    public BaseResultWithPagerDto<List<CustomAccountDto>> searchList(@RequestBody @Validated CustomLoginSearchListEntity entity, InfoEntity infoEntity) throws Exception {

        if (entity.getLogin() == null && entity.getCId() == null && Strings.isNullOrEmpty(entity.getEmail())) {
            throw ServiceException.exception(Constants.PARAMETER_ERROR);
        }

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        return customLoginFeignService.searchList(entity);
    }


    @GetMapping(value = {"/export"})
    public void exportList(@RequestParam(name = "login") Long login,
                           @RequestParam(name = "cId") Long cId,
                           @RequestParam(name = "email") String email,
                           @RequestParam(name = "loginType") Integer loginType,
                           @RequestParam(name = "pIbNo") String pIbNo,
                           @RequestParam(name = "platform") String platform,
                           @RequestParam(name = "startDate") String startDate,
                           @RequestParam(name = "endDate") String endDate,
                           @RequestParam(name = "status") Integer status,
                           HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

        CustomLoginExportListEntity entity = new CustomLoginExportListEntity();
        entity.setLogin(login);
        entity.setCId(cId);
        entity.setEmail(email);
        entity.setLoginType(loginType);
        entity.setPIbNo(pIbNo);
        entity.setPlatform(platform);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setStatus(status);

        BaseResultDto<List<com.crm.rely.backend.core.dto.custom.account.CustomAccountExportDto>> resultDto =
                customLoginFeignService.exportList(entity);
        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomAccountExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomAccountExportDto dto = new CustomAccountExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(StringUtil.getXlsxFileName("CUSTOM_LOGIN", true), dtos,
                    CustomAccountExportDto.class, response);
        }
    }

    @PostMapping("/reean/search/list")
    public ResultWithPagerDto<CustomLoginCloseReasonDto> resonPageList(@RequestBody CustomLoginCloseReasonSearchEntity entity) throws Exception {
        return customLoginFeignService.reasonPageList(entity);
    }

    @PostMapping("/operate/search/list")
    public BaseReportResultDto<List<CustomLoginOperateRecordDto>, CustomLoginFloatSumDto> operateRecordSearcherList(@RequestBody @Validated CustomLoginOperateRecordSearchEntity entity) throws Exception {
        return customLoginFeignService.operateRecordSearcherList(entity);
    }
}
