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


import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.ESystemConfig;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.base.BdSystemConfig;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.param.SystemConfigParam;
import com.hzs.system.base.service.IBdSystemConfigService;
import com.hzs.system.component.load.RedisComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 系统配置表
 * </p>
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/manager/system")
public class BdSystemConfigController extends BaseController {

    private IBdSystemConfigService systemConfigService;

    private RedisComponent redisComponent;

    @Autowired
    public void setRedisComponent(RedisComponent redisComponent) {
        this.redisComponent = redisComponent;
    }

    @Autowired
    public void setSystemConfigService(IBdSystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    /**
     * @Description: 查询系统参数
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/10/27 13:51
     */
    @Log(module = EOperationModule.SYSTEM_CONFIG_LIST, method = EOperationMethod.SELECT)
    @GetMapping("/query-sys-config-pay")
    public AjaxResult queryPaySystemConfig(String key) {
        List<BdSystemConfig> bdSystemConfigs = getBdSystemConfigs(key);
        List<SystemConfigParam> systemConfigParamList = new ArrayList<>();
        for (int i = 0; i < bdSystemConfigs.size(); i = i + 2) {
            BdSystemConfig bdSystemConfig = bdSystemConfigs.get(i);
            SystemConfigParam systemConfigParam = BeanUtil.copyProperties(bdSystemConfig, SystemConfigParam.class);
            systemConfigParam.setSystemConfigParam(BeanUtil.copyProperties(bdSystemConfigs.get(i + 1), SystemConfigParam.class));
            systemConfigParamList.add(systemConfigParam);
        }
        return AjaxResult.success(systemConfigParamList);
    }

    private List<BdSystemConfig> getBdSystemConfigs(String key) {
        List<BdSystemConfig> bdSystemConfigs = systemConfigService.querySystemConfig(key, SecurityUtils.getPkCountry());
        if (bdSystemConfigs.size() == 0) {
            for (ESystemConfig systemConfig : ESystemConfig.values()) {
                if (systemConfig.getKey().contains(key)) {
                    BdSystemConfig bdSystemConfig = new BdSystemConfig();
                    bdSystemConfig.setLabel(systemConfig.getLabel());
                    bdSystemConfig.setKey(systemConfig.getKey());
                    bdSystemConfig.setValue(systemConfig.getValue());
                    bdSystemConfig.setPkCountry(SecurityUtils.getPkCountry());
                    bdSystemConfig.setPkCreator(SecurityUtils.getUserId());
                    bdSystemConfigs.add(bdSystemConfig);
                }
            }
            systemConfigService.saveBatch(bdSystemConfigs);
        }
        return bdSystemConfigs;
    }

    /**
     * @Description: 查询系统参数
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/10/27 13:51
     */
    @Log(module = EOperationModule.SYSTEM_CONFIG_LIST, method = EOperationMethod.SELECT)
    @GetMapping("/query-sys-config")
    public AjaxResult querySystemConfig(String key) {
        List<BdSystemConfig> bdSystemConfigs = getBdSystemConfigs(key);
        List<SystemConfigParam> systemConfigParamList = new ArrayList<>();
        bdSystemConfigs.forEach(bdSystemConfig -> systemConfigParamList.add(BeanUtil.copyProperties(bdSystemConfig, SystemConfigParam.class)));
        return AjaxResult.success(systemConfigParamList);
    }

    /**
     * @Description: 查询系统参数
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/10/27 13:51
     */
    @Log(module = EOperationModule.SYSTEM_CONFIG_LIST, method = EOperationMethod.SELECT)
    @PostMapping("/update-sys-config")
    public AjaxResult saveSystemConfig(@RequestBody List<SystemConfigParam> bdSystemConfigList) {
        if (bdSystemConfigList != null && bdSystemConfigList.size() > 0) {
            bdSystemConfigList.forEach(bdSystemConfig -> {
                BdSystemConfig systemConfig = BeanUtil.copyProperties(bdSystemConfig, BdSystemConfig.class);
                if (bdSystemConfig.getSystemConfigParam() != null) {
                    BdSystemConfig itemSystemConfig = BeanUtil.copyProperties(bdSystemConfig.getSystemConfigParam(), BdSystemConfig.class);
                    updateSystemConfig(itemSystemConfig);
                }
                updateSystemConfig(systemConfig);
            });
            redisComponent.initSystemConfig();
        }
        return AjaxResult.success();
    }



    private void updateSystemConfig(BdSystemConfig systemConfig) {
        systemConfig.setPkCountry(SecurityUtils.getPkCountry());
        systemConfig.setPkModified(SecurityUtils.getUserId());
        systemConfig.setModifiedTime(new Date());
        systemConfigService.updateSystemConfig(systemConfig);
    }
}
