package com.bootdo.system.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.QueryDO;
import com.bootdo.system.service.QueryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: handle查询
 * @Author: wangp
 * @Date: 2021/6/7 18:32
 * @Return: null
 */

@Controller
@RequestMapping("/system/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    //@Autowired
    //private FeginService feginService;


    /**
     * @Description:测试handle查询
     * @Author: wangp
     * @Date: 2021/6/8 13:50
     * @Return: String
     */
    //@GetMapping(value = "test")
    //void test(HttpServletRequest request, HttpServletResponse response) {
    //    String token = request.getParameter("token");
    //    String ident = request.getParameter("ident");
    //    String type = "handle";
    //    System.out.println("token:" + token + ",ident:" + ident);
    //    JSONObject json = feginService.queryHandle(ident, token, type);
    //
    //    String data = json.getString("data");
    //    System.out.println("data:" + data);
    //    System.out.println("data:" + json.getJSONArray("data").get(2));
    //    String pretty = JSON.toJSONString(json, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
    //            SerializerFeature.WriteDateUseDateFormat);
    //    System.out.println("pretty:" + pretty);
    //}
    @Log("访问handle查询")
    @GetMapping()
    @RequiresPermissions("system:query:query")
    String Query() {
        return "system/query/query";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:query:query")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<QueryDO> queryList = queryService.list(query);
        int total = queryService.count(query);
        PageUtils pageUtils = new PageUtils(queryList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:query:add")
    String add() {
        return "system/query/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("system:query:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        QueryDO query = queryService.get(id);
        model.addAttribute("query", query);
        return "system/query/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:query:add")
    public R save(QueryDO query) {
        if (queryService.save(query) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:query:edit")
    public R update(QueryDO query) {
        queryService.update(query);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:query:remove")
    public R remove(Long id) {
        if (queryService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:query:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        queryService.batchRemove(ids);
        return R.ok();
    }

}
