package com.ruoyi.app.vip;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.utils.DateUtil;
import com.ruoyi.app.utils.GetVipqy;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.VipUser;
import com.ruoyi.system.domain.VipUserQuanyi;
import com.ruoyi.system.service.IVipUserQuanyiService;
import com.ruoyi.system.service.IVipUserService;
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.VipQuanyi;
import com.ruoyi.system.service.IVipQuanyiService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * quanyiController
 *
 * @author ruoyi
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/system/quanyi")
public class VipQuanyiController extends BaseController
{
    @Autowired
    private IVipQuanyiService vipQuanyiService;
    @Autowired
    private IVipUserService vipUserService;
    @Autowired
    private IVipUserQuanyiService vipUserQuanyiService;

    @Anonymous
    @Auth
    @GetMapping("/getMyquanyi")
    public AjaxResult getMyquanyi(@RequestHeader String token)
    {
        JwtUtil jwtUtil = new JwtUtil();
        DateUtil date = new DateUtil();
        String id = jwtUtil.getusid(token);
        QueryWrapper<VipUser> Wrapper= new QueryWrapper<>();
        Wrapper.eq("user_id",id);
        Wrapper.apply("date_format (yxtim,'%Y-%m-%d') >= date_format ({0},'%Y-%m-%d')", date.GetDate());
        VipUser user = vipUserService.getOne(Wrapper);
        if(user==null){
            return error(MessageUtils.message("no.user.not.vip"));
        }else {
            GetVipqy quany = new GetVipqy();
            List<VipQuanyi> list = quany.getvipqy(id);
            List<VipQuanyi> qulist = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                VipQuanyi vip = new VipQuanyi();
                if(list.get(i).getNum()>0){
                    vip.setId(list.get(i).getId());
                    vip.setName(list.get(i).getName());
                    vip.setNum(list.get(i).getNum());
                    vip.setVip(list.get(i).getVip());
                    vip.setShuoming(list.get(i).getShuoming());
                    vip.setTiaojian(list.get(i).getTiaojian());
                    vip.setYhfangs(list.get(i).getYhfangs());
                    vip.setYhnum(list.get(i).getYhnum());
                    qulist.add(vip);
                }
            }
            return success(qulist);
        }

    }

    /**
     * 查询quanyi列表
     */
    @PreAuthorize("@ss.hasPermi('system:quanyi:list')")
    @GetMapping("/list")
    public TableDataInfo list(VipQuanyi vipQuanyi)
    {
        startPage();
        List<VipQuanyi> list = vipQuanyiService.selectVipQuanyiList(vipQuanyi);
        return getDataTable(list);
    }

    /**
     * 导出quanyi列表
     */
    @PreAuthorize("@ss.hasPermi('system:quanyi:export')")
    @Log(title = "quanyi", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VipQuanyi vipQuanyi)
    {
        List<VipQuanyi> list = vipQuanyiService.selectVipQuanyiList(vipQuanyi);
        ExcelUtil<VipQuanyi> util = new ExcelUtil<VipQuanyi>(VipQuanyi.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.rights"));
    }

    /**
     * 获取quanyi详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:quanyi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
       VipQuanyi result= vipQuanyiService.selectVipQuanyiById(id);
        if(result!=null){
            LambdaQueryWrapper<VipUserQuanyi> query= new LambdaQueryWrapper<>();
            query.eq(VipUserQuanyi::getQuanyiId,id);
            boolean exist = vipUserQuanyiService.count(query) > 0;
            if(exist){
                result.setEdit(!exist);
            }else{
                result.setEdit(true);
            }
        }
        return success(result);
    }

    /**
     * 新增quanyi
     */
    @PreAuthorize("@ss.hasPermi('system:quanyi:add')")
    @Log(title = "quanyi", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VipQuanyi vipQuanyi)
    {
        vipQuanyi.setCreateTime(new Date());
        return toAjax(vipQuanyiService.insertVipQuanyi(vipQuanyi));
    }

    /**
     * 修改quanyi
     */
    @PreAuthorize("@ss.hasPermi('system:quanyi:edit')")
    @Log(title = "quanyi", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VipQuanyi vipQuanyi)
    {
        vipQuanyi.setUpdateTime(new Date());
        return toAjax(vipQuanyiService.updateVipQuanyi(vipQuanyi));
    }

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


}
