package com.crm.custom.controller;

import com.crm.custom.service.CustomBankService;
import com.crm.login.rely.backend.controller.BaseLoginController;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankAddEntity;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankDeleteEntity;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankUpdateEntity;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
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("/custom/bank")
public class CustomBankController extends BaseLoginController {

    @Autowired
    private CustomBankService customBankService;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated CustomBankAddEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());

        customBankService.add(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated CustomBankUpdateEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());

        customBankService.update(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated CustomBankDeleteEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());

        customBankService.delete(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file,
                                CustomInfoEntity infoEntity, HttpServletRequest req) throws Exception {

        customBankService.validated(file);

        String path = customBankService.uploadBankFile(infoEntity.getId(), file);
        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }
}
