package com.crm.pay.controller;

import com.crm.pay.service.SysRemittanceChannelService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.dto.sys.remittance.channel.SysRemittanceChannelDto;
import com.crm.rely.backend.model.dto.sys.remittance.channel.SysRemittanceChannelGetCodeDto;
import com.crm.rely.backend.model.entity.sys.remittance.channel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 入金通道信息
 */
@RestController
@RequestMapping("/remittance/channel")
public class SysRemittanceChannelController {

    @Autowired
    private SysRemittanceChannelService sysRemittanceChannelService;

    /**
     * 获取入金通道列表
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/list")
    public BaseResultDto<List<SysRemittanceChannelDto>> selectList(@RequestBody SysRemittanceChannelListEntity entity) throws Exception {

        return sysRemittanceChannelService.getAllList(entity);
    }

    /**
     * 根据code 获取通道信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/code")
    public BaseResultDto<SysRemittanceChannelDto> getByCode(@RequestBody @Validated SysRemittanceChannelByCodeEntity entity) throws Exception {

        return sysRemittanceChannelService.get(entity);
    }
    @PostMapping("/qr")
    public BaseResultDto<SysRemittanceChannelDto> getByCodeQe() throws Exception {
        SysRemittanceChannelByCodeEntity entity=new SysRemittanceChannelByCodeEntity();
        entity.setCode("QR_CODE_KEY");
        return sysRemittanceChannelService.get(entity);
    }

    @PostMapping("/manager/batch/update")
    @FeignClientAnnotation
    public BaseResultDto updateBatch(@RequestBody @Validated SysRemittanceChannelBatchUpdateEntity entity) throws Exception {
        if (entity == null) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        for (SysRemittanceChannelBasehUpdateEntity sysRemittanceChannelBasehUpdateEntity : entity.getData()) {

            if (sysRemittanceChannelBasehUpdateEntity.getValid() == null) {
                sysRemittanceChannelBasehUpdateEntity.setValid(0);
            }
        }

        sysRemittanceChannelService.save(entity, entity.getData());
        return BaseResultDto.success();
    }

    /**
     * 列表入金通道
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<SysRemittanceChannelDto> searchList(@RequestBody @Validated SysRemittanceChannelSearchEntity entity) throws Exception {


        ResultWithPagerDto<SysRemittanceChannelDto> result = sysRemittanceChannelService.searchPageList(entity);

        return result;
    }

    @PostMapping("/manager/list")
    @FeignClientAnnotation
    public BaseResultDto<List<SysRemittanceChannelDto>> list() throws Exception {

        List<SysRemittanceChannelDto> result = sysRemittanceChannelService.searchList();

        return BaseResultDto.success(result);
    }

    @PostMapping("/manager/get/code")
    @FeignClientAnnotation
    public BaseResultDto<List<SysRemittanceChannelGetCodeDto>> getCodeList() throws Exception {


        List<SysRemittanceChannelGetCodeDto> codeList = sysRemittanceChannelService.getCodeList();

        return BaseResultDto.success(codeList);
    }

    /**
     * 单个查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/manager/search/single")
    @FeignClientAnnotation
    public BaseResultDto<SysRemittanceChannelDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        SysRemittanceChannelDto result = sysRemittanceChannelService.searchSingle(entity);

        return BaseResultDto.success(result);
    }
}
