package com.fs.company.controller.wx.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.wx.sop.domain.WxSop;
import com.fs.wx.sop.service.IWxSopService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 个微SOPController
 * 
 * @author 吴树波
 * @date 2026-02-24
 */
@RestController
@RequestMapping("/wx/wxSop")
public class WxSopController extends BaseController
{
    @Autowired
    private IWxSopService wxSopService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询个微SOP列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WxSop wxSop)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wxSop.setCompanyId(loginUser.getCompany().getCompanyId());
        startPage();
        List<WxSop> list = wxSopService.selectWxSopList(wxSop);
        return getDataTable(list);
    }

    /**
     * 导出个微SOP列表
     */
    @Log(title = "个微SOP", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxSop wxSop)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wxSop.setCompanyId(loginUser.getCompany().getCompanyId());
        List<WxSop> list = wxSopService.selectWxSopList(wxSop);
        ExcelUtil<WxSop> util = new ExcelUtil<WxSop>(WxSop.class);
        return util.exportExcel(list, "个微SOP数据");
    }

    /**
     * 获取个微SOP详细信息（含执行账号 companyUserIds）
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wxSopService.selectWxSopDetailById(id));
    }

    /**
     * 新增个微SOP
     */
    @Log(title = "个微SOP", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxSop wxSop)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wxSop.setCompanyId(loginUser.getCompany().getCompanyId());
        return toAjax(wxSopService.insertWxSop(wxSop));
    }

    /**
     * 修改个微SOP
     */
    @Log(title = "个微SOP", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxSop wxSop)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wxSop.setCompanyId(loginUser.getCompany().getCompanyId());
        return toAjax(wxSopService.updateWxSop(wxSop));
    }

    /**
     * 删除个微SOP
     */
    @Log(title = "个微SOP", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxSopService.deleteWxSopByIds(ids));
    }
}
