package com.xueyi.system.resource.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.resource.domain.dto.DmH5ConfigDto;
import com.xueyi.system.resource.domain.query.DmH5ConfigQuery;
import com.xueyi.system.resource.service.IDmH5ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * H5配置表管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/h5config")
public class DmH5ConfigController extends BaseController<DmH5ConfigQuery, DmH5ConfigDto, IDmH5ConfigService> {
    private static final Logger log = LoggerFactory.getLogger(DmH5ConfigController.class);
    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "H5配置表" ;
    }

    @GetMapping("/inner/syncH5Config")
    R<String> syncH5Config(@RequestParam(value = "tId",required = false) String tId,
                           @RequestParam(value = "manCode",required = false) String manCode) {
        Long tenantId = null;
        if (StringUtils.isNotEmpty(tId)) {
            tenantId = Long.parseLong(tId);
        }
        String dmCode = null;
        if (StringUtils.isNotEmpty(manCode)) {
            dmCode = manCode;
        }
        String result = baseService.syncH5Config(tenantId, dmCode);
        if (StringUtils.isNotEmpty(result)) {
            return R.ok(result);
        }
        return R.fail("同步配置文件失败");
    }

    /**
     * 查询H5配置表列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.DM_H5_CONFIG_LIST)
    public AjaxResult list(DmH5ConfigQuery h5Config) {
        return super.list(h5Config);
    }

    /**
     * 查询H5配置表详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.DM_H5_CONFIG_SINGLE)
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.getInfo(id);
    }

    @GetMapping(value = "/{id}/{mancode}")
    @RequiresPermissions(Auth.DM_H5_CONFIG_SINGLE)
    public AjaxResult getInfo(@PathVariable("id") Serializable id,@PathVariable("mancode") String mancode) {
        DmH5ConfigDto dto = baseService.selectbyIdCode(id, mancode);
        return AjaxResult.success(dto);
    }


    /**
     * H5配置表新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.DM_H5_CONFIG_ADD)
    @Log(title = "H5配置表管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody DmH5ConfigDto h5Config) {
        log.info("H5配置表新增:{}", h5Config);
        log.info("租户:{}", h5Config.getEnterpriseId());
        return super.add(h5Config);
    }

    /**
     * H5配置表修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.DM_H5_CONFIG_EDIT)
    @Log(title = "H5配置表管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody DmH5ConfigDto h5Config) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(h5Config.getProperty());
        } catch (Exception e) {
            return AjaxResult.error("配置格式错误");
        }
        return super.edit(h5Config);
    }

    /**
     * H5配置表修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.DM_H5_CONFIG_EDIT, Auth.DM_H5_CONFIG_ES}, logical = Logical.OR)
    @Log(title = "H5配置表管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody DmH5ConfigDto h5Config) {
        return super.editStatus(h5Config);
    }

    /**
     * H5配置表批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.DM_H5_CONFIG_DEL)
    @Log(title = "H5配置表管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * H5配置表按照数字人与id删除
     */
    @DeleteMapping(value = "/{id}/{mancode}")
    @RequiresPermissions(Auth.DM_H5_CONFIG_DEL)
    @Log(title = "H5配置表管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable("id") Serializable id,@PathVariable("mancode") String manCode) {
        int result = baseService.deleteByIdCode(id, manCode);
        return AjaxResult.success(result);
    }

    /**
     * 获取H5配置表选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - H5配置表管理 - 列表 */
        String DM_H5_CONFIG_LIST = "resource:h5config:list";
        /** 系统 - H5配置表管理 - 详情 */
        String DM_H5_CONFIG_SINGLE = "resource:h5config:single";
        /** 系统 - H5配置表管理 - 新增 */
        String DM_H5_CONFIG_ADD = "resource:h5config:add";
        /** 系统 - H5配置表管理 - 修改 */
        String DM_H5_CONFIG_EDIT = "resource:h5config:edit";
        /** 系统 - H5配置表管理 - 修改状态 */
        String DM_H5_CONFIG_ES = "resource:h5config:es";
        /** 系统 - H5配置表管理 - 删除 */
        String DM_H5_CONFIG_DEL = "resource:h5config:delete";
    }
}