package com.ruoyi.demo.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.entity.bo.PlanAddBo;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.entity.vo.PlanDetailVo;
import com.ruoyi.demo.entity.vo.PlanListVo;
import com.ruoyi.demo.entity.vo.StateVo;
import com.ruoyi.demo.service.PlanService;
import com.ruoyi.demo.utils.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

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

    @PostMapping()
    public R addPlan(@RequestBody PlanAddBo bo){
        int i = planService.addPlan(bo);
        if (i == 1)
            return R.ok("广告添加成功");
        if (i == -1)
            return R.fail("无效的appkey");
        if (i == -2)
            return R.fail("无效商户");
        if (i == -3)
            return R.fail("校验sign出错");
        return R.fail("广告添加失败");
    }

    @DeleteMapping()
    public R deletePlan(PlanBo bo){
        List<String> planIds = bo.getPlanIds();
        int total = planIds.size();
        int i = planService.deletePlan(planIds);
        return R.ok("选择删除" + total + "个，成功删除" + i + "个");
    }

    // 广告管理
    // 查询广告计划列表
    @GetMapping("/list")
    public R planList(PlanBo bo){
        List<PlanListVo> vos = planService.getPlanList(bo);
        return R.ok(vos);
    }
    // 查询广告计划明细
    @GetMapping("/{planId}")
    public R planDetail(@PathVariable String planId){
        PlanDetailVo vo = planService.getPlanDetail(planId);
        return R.ok(vo);
    }
    // 投放广告计划
    // 已废弃
    @GetMapping("/push")
    public R planPush(){
        return R.ok(planService.getPlanPush());
    }

    // 广告计划管理
    // 审核广告计划
    @PutMapping("/audit")
    public R checkPlan(PlanBo bo){
        // 获取用户id和用户名
        String userId = "1";
        String planId = bo.getPlanId();
        Integer state = bo.getState();
        String auditComment = bo.getAuditComment();
        int i = planService.checkPlan(planId, state, auditComment,userId);
        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);
    }
}
