package com.crm.manager.controller;

import com.crm.manager.service.CommonService;
import com.crm.manager.service.finance.SysRemitChannelFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.system.remit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 配置出金通道
 *
 * @author houn
 */
@RestController
@RequestMapping("/remit/channel")
public class SysRemitChannelController {

    @Autowired
    private SysRemitChannelFeignService sysRemitChannelFeignService;

    @Autowired
    private CommonService commonService;
    /**
     * 添加出金通道
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysRemitChannelAddEntity entity) throws Exception {

        return  sysRemitChannelFeignService.add(entity);
    }

    /**
     * 删除出金通道
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysRemitChannelDeleteEntity entity, InfoEntity infoEntity) throws Exception {
        return  sysRemitChannelFeignService.delete(entity);
    }

    /**
     * 修改出金通道
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysRemitChannelUpdateEntity entity, InfoEntity infoEntity, HttpServletRequest request) throws Exception {
        return  sysRemitChannelFeignService.update(entity);
    }

    @PostMapping("/batch/update")
    public BaseResultDto updateBatchRate(@RequestBody @Validated SysRemitChannelBatchUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        return  sysRemitChannelFeignService.updateBatch(entity);
    }

    /**
     * 列表出金通道
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto searchList(@RequestBody @Validated SysRemitChannelSearchEntity entity) throws Exception {


        return  sysRemitChannelFeignService.searchList(entity);
    }

    @PostMapping("/list")
    public BaseResultDto list() throws Exception {

        return  sysRemitChannelFeignService.list();
    }

    @PostMapping("/get/code")
    public BaseResultDto getCodeList() throws Exception {

        return sysRemitChannelFeignService.getCodeList();
    }

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

        return  sysRemitChannelFeignService.single(entity);
    }

    @PostMapping("/{method}/upload")
    public BaseResultDto upload(@PathVariable("method") String method, @RequestParam(value = "file", required = false) MultipartFile file) throws Exception {

        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        String middlePath = "/channel/remit";
        commonService.validated(file);
        String path = commonService.uploadFile(middlePath,file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

    @PostMapping("/add/send/code")
    public BaseResultDto addChannelSendEmailCode() throws Exception {

        return  sysRemitChannelFeignService.addChannelSendEmailCode();
    }

    @PostMapping("/batch/update/send/code")
    public BaseResultDto updateChannelBatchSendEmailCode() throws Exception {

        return  sysRemitChannelFeignService.updateChannelBatchSendEmailCode();
    }

    @PostMapping("/update/send/code")
    public BaseResultDto updateChannelSendEmailCode() throws Exception {

        return  sysRemitChannelFeignService.updateChannelSendEmailCode();
    }

    @PostMapping("/delete/send/code")
    public BaseResultDto deleteChannelSendEmailCode() throws Exception {

        return  sysRemitChannelFeignService.deleteChannelSendEmailCode();
    }

}
