package tech.glinfo.enbao.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.glinfo.enbao.common.utils.PageUtils;
import tech.glinfo.enbao.common.utils.R;
import tech.glinfo.enbao.modules.sys.entity.HelpCenterContentEntity;
import tech.glinfo.enbao.modules.sys.entity.HelpCenterEntity;
import tech.glinfo.enbao.modules.sys.form.HelpCenterContentForm;
import tech.glinfo.enbao.modules.sys.form.HelpCenterForm;
import tech.glinfo.enbao.modules.sys.service.HelpCenterContentService;
import tech.glinfo.enbao.modules.sys.service.HelpCenterService;

import java.util.Arrays;
import java.util.Map;



/**
 * 帮助中心
 *
 * @author linyetong
 * @email linyetong@glinfo.com
 * @date 2022-02-14 16:39:15
 */
@RestController
@RequestMapping("sys/helpcenter")
public class HelpCenterController {
    @Autowired
    private HelpCenterService helpCenterService;

    @Autowired
    private HelpCenterContentService helpCenterContentService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = helpCenterService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		HelpCenterEntity helpCenter = helpCenterService.getById(id);

        return R.ok().put("helpCenter", helpCenter);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HelpCenterForm form){
        HelpCenterEntity helpCenter = new HelpCenterEntity();
        helpCenter.setTitle(form.getTitle());
		helpCenterService.save(helpCenter);
        for (HelpCenterContentForm content : form.getContents()) {
            HelpCenterContentEntity contentEntity = new HelpCenterContentEntity();
            contentEntity.setHelpId(helpCenter.getId());
            contentEntity.setContent(JSON.toJSONString(content));
            helpCenterContentService.save(contentEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HelpCenterEntity helpCenter){
		helpCenterService.updateById(helpCenter);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		helpCenterService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
