package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Entrust;
import com.example.demo.entity.User;
import com.example.demo.service.EntrustService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/entrust")
public class EntrustController {

    @Autowired
    private EntrustService entrustService;

    // 用户发布一个委托
    @RequestMapping("/add")
    public String add(@RequestBody Map<String,Object> entrustMap){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if(null!=user){
            Entrust entrust=new Entrust();
            entrust.setEGoodsName((String) entrustMap.get("eGoodsName"));
            entrust.setEGoodsIntro((String) entrustMap.get("eGoodsIntro"));
            entrust.setEGoodsCategory((Integer) entrustMap.get("eGoodsCategory"));
            entrust.setEGoodsCoverImg((String) entrustMap.get("eGoodsCoverImg"));
            entrust.setEGoodsCarousel((String) entrustMap.get("eGoodsCarousel"));
            entrust.setEGoodsDetailContent((String) entrustMap.get("eGoodsDetailContent"));
            entrust.setEGoodsPrice(Double.parseDouble((String) entrustMap.get("eGoodsPrice")));
            entrust.setEStockNum((Integer) entrustMap.get("eStockNum"));
            System.out.println(entrust);
            entrustService.save(entrust);
            return entrust.getEntrustId().toString();
        }else {
            return "fail";
        }
    }

    // 列出所有委托
    @RequestMapping("/list")
    public String list(){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        boolean isAdmin=SecurityUtils.getSubject().hasRole("admin");
        if(null!=user){
            if(isAdmin){
                List<Entrust> entrustList=entrustService.list();
                return JSON.toJSONString(entrustList);
            }else {
                return "fail";
            }
        }else {
            return "fail";
        }
    }

    // 根据用户id列出委托
    @RequestMapping("/listById")
    public String listById(@RequestBody Map<String,Object> map){
        Integer id= (Integer) map.get("entrustId");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        System.out.println("编辑委托："+id);
        boolean isAdmin=SecurityUtils.getSubject().hasRole("admin");
        if(null!=user){
            if(isAdmin){
                QueryWrapper<Entrust> entrustQueryWrapper = new QueryWrapper<>();
                entrustQueryWrapper.eq("entrust_id",id);
                Entrust entrust=entrustService.getOne(entrustQueryWrapper);
                return JSON.toJSONString(entrust);
            }else {
                return "fail";
            }
        }else {
            return "fail";
        }
    }
}
