package com.fjs.scenic.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.service.system.ManageService;
import com.fjs.scenic.service.user.StatisticalPermissionsService;
import com.fjs.scenic.utils.HttpReq;
import com.fjs.scenic.utils.ReturnResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * This class is used for ...
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@RestController
@RequestMapping("/leaderManage")
public class LeaderManageController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ManageService manageService;
    @Resource StatisticalPermissionsService statisticalPermissionsService;
    @Value("${pw.api}")
    private String url;

    @GetMapping("/index")
    public ModelAndView sceniclist() {
        return new ModelAndView("system/leaderManage");
    }

    /**
     * 景区列表
     */
    @PostMapping({"/getSupplier"})
    public ReturnResult getSupplierByList() {
        JsonObject jsonObject = new JsonObject();
        JSONObject jsonData = null;

        try {
            jsonData = HttpReq.sendPost(this.url, "supplierList", jsonObject.toString());
        } catch (Exception var7) {
            this.log.error(var7.getMessage());
            return ReturnResult.error(var7.getMessage());
        }

        String code = (String)jsonData.get("code");
        if (code.equals("200")) {
            String dataStr = null;

            try {
                dataStr = HttpReq.getDataStr(jsonData);
            } catch (Exception var6) {
                this.log.error(var6.getMessage());
                return ReturnResult.error(var6.getMessage());
            }

            return ReturnResult.ok(JSONArray.parseArray(dataStr));
        } else {
            return ReturnResult.error(jsonData.get("describe"));
        }
    }

    /**
     * 获取用户统计权限信息
     *
     * @param id
     * @return
     */
    @PostMapping("/getLeaderPermissonsById")
    public ReturnResult getLeaderPermissonsById(Integer id) {
        return ReturnResult.ok(statisticalPermissionsService.getByUserID(id));
    }
    /**
     * 查出全部权限
     */
    @PostMapping("/getAllPermissons")
    public ReturnResult getAllPermissons() {
        return ReturnResult.ok(statisticalPermissionsService.getAll());
    }

    /**
     * 分页
     *
     * @param currentPage 页码
     * @param pageSize    大小
     * @return
     */
    @PostMapping("/page")
    public ReturnResult page(int currentPage, int pageSize) {
        currentPage = currentPage == 0 ? 1 : currentPage;
        pageSize = pageSize == 0 ? 10 : pageSize;
        PageHelper.startPage(currentPage, pageSize);
        List<Manage> manageServices = manageService.queryAll1();
        PageInfo<Manage> pageInfo = new PageInfo(manageServices);
        return ReturnResult.ok(pageInfo);
    }


    /**
     * 删除
     */
    @PostMapping("/del/{id}")
    public ReturnResult delByOpenid(@PathVariable Integer id) {
        return ReturnResult.ok(manageService.delete1(id));
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public ReturnResult update(Manage manage) {
        return ReturnResult.ok(manageService.update1(manage));
    }

    /**
     * 冻结
     *
     * @return
     */
    @PostMapping("/updateStatus")
    public ReturnResult updateStatus(Integer id,String status) {

        return ReturnResult.ok(manageService.updateStatus1(id,status));
    }
    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/add")
    public ReturnResult add(Manage manage) {
        return ReturnResult.ok(manageService.add1(manage));
    }
    /**
     * 获取单个领导
     *
     * @return
     */
    @PostMapping("/queryOne")
    public ReturnResult queryOne(Integer id) {
        return ReturnResult.ok(manageService.queryOne1(id));
    }
    /**
     * 验证手机号是否重复
     *
     * @return
     */
    @PostMapping("/queryByPhone")
    public ReturnResult queryByPhone(String phone) {
        return ReturnResult.ok(manageService.queryByPhone1(phone));
    }
    /**
     * 验证帐号是否重复
     *
     * @return
     */
    @PostMapping("/queryByAccount")
    public ReturnResult queryByAccount(String account) {
        return ReturnResult.ok(manageService.queryByAccount(account));
    }

}
