package com.ruoyi.app.vip;

import java.text.ParseException;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.utils.DateUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.domain.VipMembership;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.service.IVipMembershipService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VipUser;
import com.ruoyi.system.service.IVipUserService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * viplistController
 *
 * @author ruoyi
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/system/vipuser")
public class VipUserController extends BaseController
{
    @Autowired
    private IVipUserService vipUserService;
    @Autowired
    private IVipMembershipService vipMembershipService;
    @Autowired
    private IInfoUserService infoUserService;

    //购买会员卡
    @Anonymous
    @Auth
    @RepeatSubmit
    @GetMapping("/adduservip")
    public AjaxResult adduservip(@RequestHeader String token,@RequestParam Long id)
    {
        JwtUtil jwtUtil = new JwtUtil();
        QueryWrapper<VipUser> queryWrapper= new QueryWrapper<>();
        String userid = jwtUtil.getusid(token);
        InfoUser infoUser = infoUserService.getById(userid);
        VipMembership vipMembership = vipMembershipService.getById(id);
        queryWrapper.eq("user_id",userid);
        VipUser vipUser = vipUserService.getOne(queryWrapper);
        DateUtil date = new DateUtil();
        if(vipUser==null){
            try {
                VipUser neus = new VipUser();
                String dqriq = date.addDate(date.GetDate(), Math.toIntExact(vipMembership.getDuration()));
                neus.setUserId(Long.valueOf(userid));
                neus.setYxtim(dqriq);
                neus.setPhone(infoUser.getPhone());
                neus.setName(infoUser.getNickName());
                vipUserService.saveOrUpdate(neus);
                return success();
            } catch (ParseException e) {
                e.printStackTrace();
                return error();
            }
        }else {
            boolean orn = date.isPastDate(vipUser.getYxtim());
            if(orn){
                try {
                    String dqriq = date.addDate(date.GetDate(), Math.toIntExact(vipMembership.getDuration()));
                    vipUser.setUserId(Long.valueOf(userid));
                    vipUser.setYxtim(dqriq);
                    vipUser.setPhone(infoUser.getPhone());
                    vipUser.setName(infoUser.getNickName());
                    vipUserService.saveOrUpdate(vipUser);
                    return success();
                } catch (ParseException e) {
                    e.printStackTrace();
                    return error();
                }
            }else {
                try {
                    String dqriq = date.addDate(vipUser.getYxtim(), Math.toIntExact(vipMembership.getDuration()));
                    vipUser.setUserId(Long.valueOf(userid));
                    vipUser.setYxtim(dqriq);
                    vipUser.setPhone(infoUser.getPhone());
                    vipUser.setName(infoUser.getNickName());
                    vipUserService.saveOrUpdate(vipUser);
                    return success();
                } catch (ParseException e) {
                    e.printStackTrace();
                    return error();
                }
            }
        }
    }
    /**
     * 查询viplist列表
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:list')")
    @GetMapping("/list")
    public TableDataInfo list(VipUser vipUser)
    {
        startPage();
        List<VipUser> list = vipUserService.selectVipUserList(vipUser);
        return getDataTable(list);
    }

    /**
     * 导出viplist列表
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:export')")
    @Log(title = "viplist", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VipUser vipUser)
    {
        List<VipUser> list = vipUserService.selectVipUserList(vipUser);
        ExcelUtil<VipUser> util = new ExcelUtil<VipUser>(VipUser.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.viplist"));
    }

    /**
     * 获取viplist详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vipUserService.selectVipUserById(id));
    }

    /**
     * 新增viplist
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:add')")
    @Log(title = "viplist", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VipUser vipUser)
    {
        return toAjax(vipUserService.insertVipUser(vipUser));
    }

    /**
     * 修改viplist
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:edit')")
    @Log(title = "viplist", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VipUser vipUser)
    {
        return toAjax(vipUserService.updateVipUser(vipUser));
    }

    /**
     * 删除viplist
     */
    @PreAuthorize("@ss.hasPermi('system:vipuser:remove')")
    @Log(title = "viplist", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vipUserService.deleteVipUserByIds(ids));
    }
}
