package com.ruoyi.web.controller.cp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WeChatUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CpsUsrApply;
import com.ruoyi.system.service.ICpsUsrApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * cps_usr_apply与会申请Controller
 *
 * @author ruoyi
 * @date 2024-09-03
 */
@RestController
@RequestMapping("/cp/usrApply")
public class CpsUsrApplyController extends BaseController
{
    @Autowired
    private ICpsUsrApplyService cpsUsrApplyService;

    /**
     * 查询cps_usr_apply与会申请列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('cp:apply:index')")
    public TableDataInfo list(CpsUsrApply cpsUsrApply)
    {
        startPage();
        List<CpsUsrApply> list = cpsUsrApplyService.selectCpsUsrApplyList(cpsUsrApply);
        return getDataTable(list);
    }

    @GetMapping("/regist/list")
    public TableDataInfo registList() {
        startPage();
        WeChatUser loginUser = (WeChatUser) getLoginUser();
        List<CpsUsrApply> cpsUsrApplies = cpsUsrApplyService.selectCpsUsrApplyList(new CpsUsrApply() {{
            setUsrWechatId(loginUser.getUserId());
        }});
        return getDataTable(cpsUsrApplies);
    }


    /**
     * 导出cps_usr_apply与会申请列表
     */
    @Log(title = "cps_usr_apply与会申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpsUsrApply cpsUsrApply)
    {
        List<CpsUsrApply> list = cpsUsrApplyService.selectCpsUsrApplyList(cpsUsrApply);
        ExcelUtil<CpsUsrApply> util = new ExcelUtil<CpsUsrApply>(CpsUsrApply.class);
        util.exportExcel(response, list, "cps_usr_apply与会申请数据");
    }

    /**
     * 获取cps_usr_apply与会申请详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cpsUsrApplyService.selectCpsUsrApplyById(id));
    }

    @GetMapping(value = "/info/meeting")
    public AjaxResult getUsrMeetingInfo() {
        return success(cpsUsrApplyService.selectCpsUsrApplyByTel());
    }

    @GetMapping(value = "/info/meeting/{type}")
    public AjaxResult getUsrThemeMeetingInfo(@PathVariable("type") String type) {
        return success(cpsUsrApplyService.selectCpsUsrApplyThemeMeeting(type));
    }

    /**
     * 新增cps_usr_apply与会申请
     */
    @Log(title = "cps_usr_apply与会申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpsUsrApply cpsUsrApply)
    {
        cpsUsrApply.setCreateTime(DateUtils.getNowDate());
        return toAjax(cpsUsrApplyService.insertCpsUsrApply(cpsUsrApply));
    }

    /**
     * 修改cps_usr_apply与会申请
     */
    @Log(title = "cps_usr_apply与会申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpsUsrApply cpsUsrApply)
    {
        return toAjax(cpsUsrApplyService.updateCpsUsrApply(cpsUsrApply));
    }


    @Log(title = "cps_usr_apply与会申请", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody CpsUsrApply cpsUsrApply) {
        return toAjax(cpsUsrApplyService.rejectCpsUsrApply(cpsUsrApply));
    }

    /**
     * 删除cps_usr_apply与会申请
     */
    @Log(title = "cps_usr_apply与会申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cpsUsrApplyService.deleteCpsUsrApplyByIds(ids));
    }
}
