package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.ext.CurrencyDto;
import com.fuint.common.param.PageParam;
import com.fuint.common.service.CurrencyService;
import com.fuint.common.util.I18nUtil;
import com.fuint.common.util.TokenUtil;
import com.fuint.common.vo.CurrencyVo;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.PageResult;
import com.fuint.framework.web.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货币表管理类controller
 *
 * Created by pzg
 * CopyRight https://www.fuint.cn
 */
@Api(tags="管理端-货币表相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/currency")
public class BackendCurrencyController extends BaseController {

    /**
     * 货币表服务接口
     */
    private CurrencyService currencyService;

    /**
     * 货币表列表查询
     *
     * @return 货币表列表
     */
    @ApiOperation(value = "货币表列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('currency:list')")
    public PageResult<CurrencyVo> list(@RequestParam(defaultValue = "") String name, PageParam pageParam) throws BusinessCheckException {
        return currencyService.getCurrencyById(name,pageParam);
    }

    /**
     * 更新货币数据
     *
     * @return 结果
     */
    @ApiOperation(value = "更新货币数据")
    @RequestMapping(value = "/updateCurrency", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('currency:edit')")
    public ResponseObject updateStatus(@RequestBody CurrencyDto currencyDto) throws BusinessCheckException {
        boolean success = currencyService.updateCurrency(currencyDto);
        return success?getSuccessResult(true):getFailureResult(1002, I18nUtil.getMessage("updateErr"));
    }

    /**
     * 保存货币表
     * @param currencyDto 参数实体
     * @return 结果
     * @throws BusinessCheckException
     */
    @ApiOperation(value = "保存货币表")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('currency:add')")
    public ResponseObject saveHandler(@RequestBody CurrencyDto currencyDto) throws BusinessCheckException {
        boolean success = currencyService.addCurrency(currencyDto);
        return success?getSuccessResult(true):getFailureResult(1002,I18nUtil.getMessage("saveErr"));
    }

    /**
     * 通过id删除货币数据
     *
     * @param id id
     * @return 结果
     */
    @ApiOperation(value = "通过id删除货币数据")
    @DeleteMapping("/{id}")
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('currency:delete')")
    public ResponseObject info(@PathVariable("id") Integer id) throws BusinessCheckException {
        boolean success = currencyService.deleteCurrency(id);
        return success?getSuccessResult(true):getFailureResult(1002,I18nUtil.getMessage("deleteErr"));
    }


    /**
     * 获取全部的货币列表
     *
     * @param name 货币名称
     * @return 结果
     */
    @ApiOperation(value = "获取全部的货币列表")
    @DeleteMapping("/allList")
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('currency:list')")
    public ResponseObject allList(@RequestParam(value = "name",defaultValue = "") String name) throws BusinessCheckException {
        List<CurrencyVo> result = currencyService.allList(name);
        return getSuccessResult(result);
    }


}
