package com.crm.manager.controller;

import com.crm.manager.dto.export.CustomActivityDepositRaffleExportDto;
import com.crm.manager.dto.export.CustomActivityDepositRaffleNumberRecordExportDto;
import com.crm.manager.dto.export.CustomActivityDepositRaffleRecordExportDto;
import com.crm.manager.service.activity.CustomActivityDepositRaffleFeignService;
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.deposit.raffle.CustomActivityDepositRaffleDto;
import com.crm.rely.backend.core.dto.custom.activity.deposit.raffle.CustomActivityDepositRaffleNumberRecordDto;
import com.crm.rely.backend.core.dto.custom.activity.deposit.raffle.CustomActivityDepositRaffleRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.CustomActivityDepositRaffleSearchEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.CustomActivityDepositRaffleUpdateConfigWeekRaffleEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.CustomActivityDepositRaffleUpdateNumberEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.raffle.CustomActivityDepositRaffleRecordSearchEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.raffle.number.CustomActivityDepositRaffleNumberRecordSearchEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.raffle.week.CustomActivityDepositRaffleWeekApproveEntity;
import com.crm.rely.backend.core.entity.custom.activity.deposit.raffle.week.CustomActivityDepositRaffleWeekSearchEntity;
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 lombok.extern.log4j.Log4j;
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
 */
@Log4j
@RestController
@RequestMapping("/custom/activity/deposit/raffle")
public class CustomActivityDepositRaffleController {

    @Autowired
    private CustomActivityDepositRaffleFeignService service;

    @PostMapping("/update/number")
    public BaseResultDto updateNumber(@RequestBody @Validated CustomActivityDepositRaffleUpdateNumberEntity entity) throws Exception {

        return service.updateNumber(entity);
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomActivityDepositRaffleDto> searcherList(@RequestBody @Validated CustomActivityDepositRaffleSearchEntity 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 =
                    "cId") Long cId,
            @RequestParam(name =
                    "email") String email,
            @RequestParam(name =
                    "startDate") String startDate,
            @RequestParam(name =
                    "endDate") String endDate,
            HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityDepositRaffleSearchEntity entity =
                new CustomActivityDepositRaffleSearchEntity();

        entity.setCId(cId);
        entity.setEmail(email);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

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

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityDepositRaffleExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityDepositRaffleExportDto dto = new CustomActivityDepositRaffleExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

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

        }
    }

    @PostMapping("/record/search/list")
    public ResultWithPagerDto<CustomActivityDepositRaffleRecordDto> searcherRecordList(@RequestBody @Validated CustomActivityDepositRaffleRecordSearchEntity entity) throws Exception {

        return service.searcherRecordList(entity);
    }

    @PostMapping("/record/search/single")
    public BaseResultDto searcherRecordSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.searcherRecordSingle(entity);
    }

    @RequestMapping("/record/export")
    public void exportRecord(
            @RequestParam(name =
                    "cId") Long cId,
            @RequestParam(name =
                    "email") String email,
            @RequestParam(name =
                    "raffleId") Long raffleId,
            @RequestParam(name =
                    "startDate") String startDate,
            @RequestParam(name =
                    "endDate") String endDate,
            HttpServletResponse response) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityDepositRaffleRecordSearchEntity entity =
                new CustomActivityDepositRaffleRecordSearchEntity();

        entity.setCId(cId);
        entity.setEmail(email);
        entity.setRaffleId(raffleId);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

        BaseResultDto<List<CustomActivityDepositRaffleRecordDto>> resultDto =
                service.exportRecord(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityDepositRaffleRecordExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityDepositRaffleRecordExportDto dto = new CustomActivityDepositRaffleRecordExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

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

        }
    }

    @PostMapping("/number/record/search/list")
    public ResultWithPagerDto<CustomActivityDepositRaffleNumberRecordDto> searcherNumberRecordList(@RequestBody @Validated CustomActivityDepositRaffleNumberRecordSearchEntity entity) throws Exception {

        return service.searcherNumberRecordList(entity);
    }

    @PostMapping("/number/record/search/single")
    public BaseResultDto searcherNumberRecordSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.searcherNumberRecordSingle(entity);
    }

    @PostMapping("/number/record/export")
    public void exportNumberRecord(
            @RequestParam(name =
                    "cId") Long cId,
            @RequestParam(name =
                    "email") String email,
            @RequestParam(name =
                    "startDate") String startDate,
            @RequestParam(name =
                    "endDate") String endDate,
            HttpServletResponse response) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        CustomActivityDepositRaffleNumberRecordSearchEntity entity =
                new CustomActivityDepositRaffleNumberRecordSearchEntity();

        entity.setCId(cId);
        entity.setEmail(email);
        entity.setStartDate(Strings.isNullOrEmpty(startDate) ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(Strings.isNullOrEmpty(endDate) ? null : simpleDateFormat.parse(endDate));
        entity.setExport(1);

        BaseResultDto<List<CustomActivityDepositRaffleNumberRecordDto>> resultDto =
                service.exportNumberRecord(entity);

        if (resultDto != null && resultDto.getCode() == 200) {
            List<CustomActivityDepositRaffleNumberRecordExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                CustomActivityDepositRaffleNumberRecordExportDto dto =
                        new CustomActivityDepositRaffleNumberRecordExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dto.setPIBNO(item.getPIbNo());

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

        }
    }

    @PostMapping("/week/markets/search/list")
    public BaseResultDto marketsWeekSearchList(@RequestBody @Validated CustomActivityDepositRaffleWeekSearchEntity entity) throws Exception {
        entity.setStatus(2);
        return service.managerWeekSearchList(entity);
    }

    @PostMapping("/week/manager/search/list")
    public BaseResultDto managerWeekSearchList(@RequestBody @Validated CustomActivityDepositRaffleWeekSearchEntity entity) throws Exception {
        return service.managerWeekSearchList(entity);
    }

    @PostMapping("/markets/week/approve")
    public BaseResultDto marketsApprove(@RequestBody @Validated CustomActivityDepositRaffleWeekApproveEntity entity) throws Exception {


        return service.marketsApprove(entity);
    }

    @PostMapping("/week/approve")
    public BaseResultDto managerApprove(@RequestBody @Validated CustomActivityDepositRaffleWeekApproveEntity entity) throws Exception {

        return service.managerApprove(entity);
    }

    @PostMapping("/manager/week/search/single")
    public BaseResultDto managerSearchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return service.managerWeekSearchSingle(entity);
    }

    @RequestMapping("/get/config/week/raffle")
    public BaseResultDto getConfigWeekRaffle() throws Exception {

        return service.getConfigWeekRaffle();
    }

    @PostMapping("/update/config/week/raffle")
    public BaseResultDto updateConfigWeekRaffle(@RequestBody @Validated CustomActivityDepositRaffleUpdateConfigWeekRaffleEntity entity) throws Exception {

        return service.updateConfigWeekRaffle(entity);
    }

    @PostMapping("/week")
    public BaseResultDto managerWeek() throws Exception {
        return service.managerWeek();
    }

}