package com.sifang.modules.setting.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.req.EditProcessSetReq;
import com.sifang.modules.setting.service.EntryProcessSetService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.FormSetUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author DQChen
 * @date 2024-02-02 16:21
 */
@Controller
@RequestMapping(value = "${adminPath}/set/buildProcess")
public class EntryProcessSetController extends BaseController {

    @Autowired
    EntryProcessSetService processSetService;

    /**
     * 建站流程是这页面
     * @param model
     * @return
     */
    @RequestMapping("setting")
    public String toProcessSetPage(Model model) {
        model.addAttribute("partnerStep", FormSetUtils.findAllPartnerStep());
        return "modules/setting/process/entryProcessSet";
    }

    /**
     * 建站流程设置数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("processSetData")
    public String processSetData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        String partnerId = request.getParameter("partnerId");
        if (StringUtil.isBlank(partnerId)) {
            partnerId = "01";
        }
        List<Record> list = processSetService.selectSetForPartner(partnerId);
        page.setData(list);
        page.setCount((long) list.size());
        return page.renderJson();
    }

    /**
     * 启用/禁用
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("editPartnerProcessSet")
    public Result editPartnerProcessSet(@RequestBody EditProcessSetReq req){
        try {
            return processSetService.editProcessSet(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("操作失败");
    }


}
