package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.ext.CountryDto;
import com.fuint.common.service.CountryService;
import com.fuint.common.util.AuthUserUtil;
import com.fuint.common.util.I18nUtil;
import com.fuint.common.vo.CountryVo;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
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/country")
public class BackendCountryController extends BaseController {

    /**
     * 国家表服务接口
     */
    private CountryService countryService;

    /**
     * 国家表列表查询
     *
     * @return 国家表列表
     */
    @ApiOperation(value = "国家表树形结构查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('country:list')")
    public ResponseObject list(@RequestParam(value = "countryCode",defaultValue = "") String countryCode,
                               @RequestParam(value = "countryName",defaultValue = "") String countryName) throws BusinessCheckException {
        List<CountryVo> countryVoList = countryService.getCountryTree(countryCode,countryName);
        return getSuccessResult(countryVoList);
    }

    /**
     * 更新国家表状态
     *
     * @return
     */
    @ApiOperation(value = "更新国家表状态")
    @RequestMapping(value = "/updateCountry", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('country:edit')")
    public ResponseObject updateCountry(CountryDto countryDto) throws BusinessCheckException {
        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            return getFailureResult(1001, I18nUtil.getMessage("notAuthenticated"));
        }
        Long accountInfoId = accountInfo.getId();
        boolean result = countryService.updateCountry(countryDto,accountInfoId);
        return result ?getSuccessResult(true):getFailureResult(1002,I18nUtil.getMessage("updateErr"));
    }

    /**
     * 保存国家表
     *
     * @return
     */
    @ApiOperation(value = "保存国家表")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('country:add')")
    public ResponseObject saveHandler(@RequestBody CountryDto countryDto) throws BusinessCheckException {
        AccountInfo accountInfo = AuthUserUtil.get();
        if (accountInfo == null) {
            return getFailureResult(1001, I18nUtil.getMessage("notAuthenticated"));
        }
        Long accountInfoId = accountInfo.getId();
        boolean result = countryService.saveCountry(countryDto,accountInfoId);
        return result?getSuccessResult(true):getFailureResult(1002,I18nUtil.getMessage("saveErr"));
    }

    /**
     * 删除国家表数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除国家表数据")
    @DeleteMapping("/{id}")
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('country:list')")
    public ResponseObject deleteCountry(@PathVariable Long id) throws BusinessCheckException {
        boolean success = countryService.deleteCountry(id);
        return success ? getSuccessResult(true):getFailureResult(1002,I18nUtil.getMessage("deleteErr"));
    }
}
