package com.crm.manager.controller;

import com.crm.manager.service.SysBankService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.constant.ManagerConstant;
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.system.bank.SysBankListDto;
import com.crm.rely.backend.core.dto.system.bank.SysBankSearchListDto;
import com.crm.rely.backend.core.dto.system.remit.SysRemitChannelSearchDto;
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.bank.SysBankAddEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankDeleteEntitys;
import com.crm.rely.backend.core.entity.system.bank.SysBankSearchListEntity;
import com.crm.rely.backend.core.entity.system.bank.SysBankUpdateEntity;
import com.crm.rely.backend.core.entity.system.channel.bank.SysChannelBankSaveEntity;
import com.crm.rely.backend.core.entity.system.config.SendUpdateEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.remit.*;
import com.crm.rely.backend.util.GetIpAndMac;
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;
import java.util.Date;
import java.util.List;

/**
 * 银行列表
 *
 * @author houn
 */
@RestController
@RequestMapping("/bank")
public class SysBankController {

    @Autowired
    private SysBankService sysBankService;

    /**
     * 添加银行
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysBankAddEntity entity) throws Exception {

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

        if (entity.getSubIndex() == null) {
            entity.setSubIndex(0);
        }

        sysBankService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除银行
     *
     * @param entitys
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysBankDeleteEntitys entitys) throws Exception {

        sysBankService.delete(entitys);
        return BaseResultDto.success();
    }

    /**
     * 修改银行
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysBankUpdateEntity entity) throws Exception {

        if (entity.getValid() == null) {
            entity.setValid(0);
        }
        if (entity.getSubIndex() == null) {
            entity.setSubIndex(0);
        }

        sysBankService.update(entity);
        return BaseResultDto.success();
    }


    /**
     * 列表银行
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public ResultWithPagerDto<SysBankSearchListDto> searchList(@RequestBody @Validated SysBankSearchListEntity entity) throws Exception {


        ResultWithPagerDto<SysBankSearchListDto> result = sysBankService.searchPageList(entity);

        return result;
    }

    @PostMapping("/list")
    public BaseResultDto<List<SysBankListDto>> list() throws Exception {
        List<SysBankListDto> result = sysBankService.getList();
        return BaseResultDto.success(result);
    }

    @PostMapping("/custom/list")
    @FeignClientAnnotation
    public BaseResultDto<List<SysBankListDto>> customList() throws Exception {

        List<SysBankListDto> result = sysBankService.getList();

        return BaseResultDto.success(result);
    }

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

        SysBankSearchListDto result = sysBankService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

    @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.SYSTEM_ERROR);
        }
        sysBankService.validated(file);

        String path = sysBankService.uploadFile(file);

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