package com.ruoyi.web.controller.cp;

import com.ruoyi.common.annotation.Anonymous;
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.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.CpsContentInfo;
import com.ruoyi.system.domain.CpsMeetingUsr;
import com.ruoyi.system.rest.WechatService;
import com.ruoyi.system.service.ICpsMeetingUsrService;
import com.ruoyi.web.core.config.WeChatCfg;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cps_meeting_usr与会人员信息Controller
 *
 * @author ruoyi
 * @date 2024-08-18
 */
@RestController
@CrossOrigin
@RequestMapping("/cp/usr")
public class CpsMeetingUsrController extends BaseController {
    @Autowired
    private ICpsMeetingUsrService cpsMeetingUsrService;

    @Autowired
    private WeChatCfg weChatCfg;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/wx/login")
    @Anonymous
    public AjaxResult login(@RequestBody WeChatUser wxUser) {
        Map session = wechatService.getSession(weChatCfg.getAppid(), weChatCfg.getAppSecret(), wxUser.getCode());
        String errcode = MapUtils.getString(session, "errcode");
        if (errcode != null) {
            String errmsg = MapUtils.getString(session, "errmsg");
            return AjaxResult.error(errmsg);
        }
        String openId = MapUtils.getString(session, "openid");
        String sessionKey = MapUtils.getString(session, "session_key");
        wxUser.setOpenid(openId);
        wxUser.setSessionKey(sessionKey);
        CpsMeetingUsr weChatUser = cpsMeetingUsrService.getWeChatUser(wxUser);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(wxUser.getOpenid(), wxUser.getSessionKey());
        authenticationToken.setDetails(wxUser);
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String token = tokenService.createToken(wxUser);
        // 生成token
        return AjaxResult.success(new HashMap<String, Object>() {{
            put("token", token);
            put("user", weChatUser);
        }});
    }

    /**
     * 查询cps_meeting_usr与会人员信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CpsMeetingUsr cpsMeetingUsr) {
        startPage();
        List<CpsMeetingUsr> list = cpsMeetingUsrService.selectCpsMeetingUsrList(cpsMeetingUsr);
        return getDataTable(list);
    }

    /**
     * 导出cps_meeting_usr与会人员信息列表
     */
    @Log(title = "cps_meeting_usr与会人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CpsMeetingUsr cpsMeetingUsr) {
        List<CpsMeetingUsr> list = cpsMeetingUsrService.selectCpsMeetingUsrList(cpsMeetingUsr);
        ExcelUtil<CpsMeetingUsr> util = new ExcelUtil<CpsMeetingUsr>(CpsMeetingUsr.class);
        util.exportExcel(response, list, "cps_meeting_usr与会人员信息数据");
    }

    /**
     * 获取cps_meeting_usr与会人员信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cpsMeetingUsrService.selectCpsMeetingUsrById(id));
    }

    /**
     * 新增cps_meeting_usr与会人员信息
     */
    @Log(title = "cps_meeting_usr与会人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpsMeetingUsr cpsMeetingUsr) {
        return toAjax(cpsMeetingUsrService.insertCpsMeetingUsr(cpsMeetingUsr));
    }

    @Log(title = "与会人员报名", businessType = BusinessType.INSERT)
    @PostMapping(value = "/regist")
    public AjaxResult usrRegist(@RequestBody CpsMeetingUsr cpsMeetingUsr) {
        cpsMeetingUsrService.usrRegister(cpsMeetingUsr);
        return AjaxResult.success();
    }

    /**
     * 修改cps_meeting_usr与会人员信息
     */
    @Log(title = "cps_meeting_usr与会人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpsMeetingUsr cpsMeetingUsr) {
        return toAjax(cpsMeetingUsrService.updateCpsMeetingUsr(cpsMeetingUsr));
    }

    /**
     * 删除cps_meeting_usr与会人员信息
     */
    @Log(title = "cps_meeting_usr与会人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cpsMeetingUsrService.deleteCpsMeetingUsrByIds(ids));
    }

    @Log(title = "用户赞收藏信息", businessType = BusinessType.DELETE)
    @GetMapping("/content/rel/{openId}")
    public TableDataInfo usrContentRel(CpsContentInfo cpsContentInfo, @PathVariable String openId) {
        startPage();
        List<CpsContentInfo> list = cpsMeetingUsrService.qryUsrCollectContent(openId, cpsContentInfo.getTitle());
        return getDataTable(list);
    }

    @Log(title = "用户行程信息", businessType = BusinessType.DELETE)
    @GetMapping("/trip/{openId}")
    public AjaxResult usrContentRel(@PathVariable String openId) {
        return AjaxResult.success(cpsMeetingUsrService.qryUsrTripContent(openId));
    }
}
