package com.hzs.system.config.controller.manage;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.config.BdRegisterPage;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdRegisterPageService;
import com.hzs.system.config.vo.RegisterPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: 注册页面配置控制器
 * @Author: jiang chao
 * @Time: 2022/10/18 17:05
 * @Classname: BdRegisterPageController
 * @PackageName: com.hzs.system.config.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/register-page")
public class BdRegisterPageController extends BaseController {

    @Autowired
    private IBdRegisterPageService iBdRegisterPageService;

    /**
     * 列表
     *
     * @return
     */
    @Log(module = EOperationModule.REGISTER_CONFIG, business = EOperationBusiness.REGISTER_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public AjaxResult list() {
        // 最终返回结果
        List<RegisterPageVO> resultList = new ArrayList<>();
        // 查询已配置列表
        List<BdRegisterPage> list = iBdRegisterPageService.queryList(SecurityUtils.getPkCountry());

        for (ERegisterPage value : ERegisterPage.values()) {
            RegisterPageVO registerPageVO = RegisterPageVO.builder()
                    .field(value.getValue())
                    .name(value.getLabel())
                    .display(EYesNo.NO.getIntValue())
                    .build();

            // 如果数据库中已经存在，则返回标记为存在
            for (BdRegisterPage registerPage : list) {
                if (value.getValue().equals(registerPage.getField())) {
                    registerPageVO.setPkId(registerPage.getPkId());
                    registerPageVO.setDisplay(registerPage.getDisplay());
                    break;
                }
            }

            resultList.add(registerPageVO);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 修改显示
     *
     * @param registerPageFieldList
     * @return
     */
    @Log(module = EOperationModule.REGISTER_CONFIG, business = EOperationBusiness.REGISTER_CONFIG, method = EOperationMethod.UPDATE)
    @PutMapping("/save")
    public AjaxResult save(@RequestBody List<String> registerPageFieldList) {
        iBdRegisterPageService.saveData(registerPageFieldList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
        return AjaxResult.success();
    }

}
