package com.ruoyi.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.demo.entity.AddrCategory;
import com.ruoyi.demo.entity.bo.CallBackBean;
import com.ruoyi.demo.entity.bo.PlanAddBo;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.entity.vo.*;
import com.ruoyi.demo.service.PlanService;
import com.ruoyi.demo.utils.StateUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController {
    @Autowired
    PlanService planService;
    @Autowired
    ISysUserService sysUserService;

    // 广告管理
    // 上下架广告计划
    @PutMapping("/{planId}")
    public R planUpDown(@PathVariable String planId){
        int i = planService.planUpDown(planId);
        if (i == 1)
            return R.ok("成功");
        if (i == -1)
            return R.fail("计划不存在");
        return R.fail("失败");
    }
    // 查询广告计划列表
    @PostMapping("/list")
    public R planList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        PageInfo<List<PlanListVo>> planList = planService.getPlanList(bo);
        return R.ok(planList);
    }
    // 查询广告计划明细
    @GetMapping("/{planId}")
    public R planDetail(@PathVariable String planId){
        return R.ok(planService.getPlanDetail(planId));
    }
    // 投放广告计划
    @GetMapping("/push/{equipmentCode}")
    public R planPush(@PathVariable String equipmentCode){
        return R.ok(planService.getPlanPushByCode(equipmentCode));
    }

    // 广告计划管理
    // 审核广告计划
    @PutMapping("/audit")
    public R checkPlan(@RequestBody PlanBo bo){
        // 获取用户id和用户名
        String loginId = String.valueOf(StpUtil.getLoginId());
        String numericPart = loginId.split(":")[1];
        Long userId = Long.valueOf(numericPart);
        SysUser sysUser = sysUserService.selectUserById(userId);
        int i = planService.checkPlan(bo,userId,sysUser.getNickName());
        if (i == 1)
            return R.ok("审核操作成功");
        if (i == -1)
            return R.fail("该计划不存在");
        return R.fail("审核操作失败");
    }
    // 下拉框——获取审核状态
    // 已废弃
    @GetMapping("/getState")
    public R getState(){
        List<StateVo> vos = new ArrayList<>();
        vos.add(new StateVo(StateUtil.NOT_AUDITED,StateUtil.NOT_AUDITED_CHAR));
        vos.add(new StateVo(StateUtil.APPROVED,StateUtil.APPROVED_CHAR));
        vos.add(new StateVo(StateUtil.NOT_APPROVED,StateUtil.NOT_APPROVED_CHAR));
        return R.ok(vos);
    }
    // 查询广告计划管理列表
    @PostMapping("/audit/list")
    public R auditList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        PageInfo<List<PlanManageListVo>> list = planService.getPlanManageList(bo);
        return R.ok(list);
    }
    // 查询广告计划管理明细
    @GetMapping("/audit/{planId}")
    public R auditDetail(@PathVariable String planId){
        return R.ok(planService.getPlanManageDetail(planId));
    }

    // 广告推送
    // 获取sign——用于测试
    @PostMapping("/getSign")
    public R getSign(@RequestBody PlanAddBo bo){
        if (bo.getPlanStartTime() != null && bo.getPlanEndTime() != null)
            if (bo.getPlanStartTime().compareTo(bo.getPlanEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        String sign = planService.getSign(bo);
        if (sign.equals("error"))
            return R.fail("无效的appkey");
        return R.ok(sign);
    }
    // 添加广告推送计划
    @PostMapping()
    public R addPlan(@RequestBody @Validated(value = {AddGroup.class}) PlanAddBo bo){
        if (bo.getPlanStartTime() != null && bo.getPlanEndTime() != null)
            if (bo.getPlanStartTime().compareTo(bo.getPlanEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        int i = planService.addPlan(bo);
        if (i == 1)
            return R.ok("广告添加成功");
        if (i == -1)
            return R.fail("无效的appkey");
        if (i == -3)
            return R.fail("校验sign出错");
        return R.fail("广告添加失败");
    }
    // 删除广告推送计划
    @DeleteMapping("/{planId}")
    public R deletePlan(@PathVariable String planId){
        int i = planService.deletePlan(planId);
        if (i == 1)
            return R.ok("删除成功");
        return R.fail("删除失败");
    }
    // 查询广告计划推送计划列表
    @PostMapping("/push/list")
    public R pushList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        PageInfo<List<PlanPushListVo>> list = planService.getPlanPushList(bo);
        return R.ok(list);
    }


    // 广告投放
    // 广告投放列表
    @PostMapping("/statistics/list")
    public R statisticsList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        PageInfo<List<PlanPushStatisticsListVo>> list = planService.getPlanPushStatisticsList(bo);
        return R.ok(list);
    }
    // 立刻进行一次统计——用于测试
    @GetMapping("/statistics/start")
    public R statisticsStart(){
        planService.planPushStatisticsStart();
        return R.ok("更新完成");
    }

    // 首页
    // 获取总广告数
    @GetMapping("/total")
    public R total(){
        return R.ok(planService.getPlanTotal());
    }

    // 其他
    // 获取设备信息
    @PostMapping("/getEquipments")
    public R getEquipments(@RequestBody PlanBo bo){
        return R.ok(planService.getEquipments(bo));
    }
    // 下拉框——获取省市区地区码
    @GetMapping("/getAddrCategory")
    public R getAddrCategory(String code,String level){
        return R.ok(planService.getAddrCategoryLevel(code,level));
    }
}
