package com.hzs.system.base.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.system.base.service.IBdCountryService;
import com.hzs.system.base.vo.CountryQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 国家控制器
 */
@RestController("apiCountryController")
@RequestMapping("/api/country")
public class ApiCountryController extends BaseController {

    @Autowired
    private IBdCountryService countryService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 查询所有国家
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(){
        QueryWrapper<BdCountry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ENABLE_STATE", EYesNo.YES.getIntValue());
        queryWrapper.orderByAsc(SystemFieldConstants.PK_ID);
        List<BdCountry> list = countryService.list(queryWrapper);

        // 获取需要翻译的枚举翻译
        Map<Integer, String> transactionMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_COUNTRY);

        List<CountryQueryVO> resultList = new ArrayList<>(list.size());
        for (BdCountry bdCountry : list) {
            String name = transactionMap.get(bdCountry.getPkTransaction());

            resultList.add(CountryQueryVO.builder()
                    .pkId(bdCountry.getPkId())
                    .code(bdCountry.getCode())
                    .shortName(StringUtils.isNotEmpty(name) ? name : bdCountry.getShortName())
                    .name(StringUtils.isNotEmpty(name) ? name : bdCountry.getName())
                    .nationalFlag1(bdCountry.getNationalFlag1())
                    .nationalFlag2(bdCountry.getNationalFlag2())
                    .build());
        }
        return AjaxResult.success(resultList);
    }

}
