package com.ff.weijinxiao.controller;


import com.ff.weijinxiao.domain.MemberAndService;
import com.ff.weijinxiao.service.MemberAndServiceService;
import com.ff.weijinxiao.util.JsonTool;
import com.ff.weijinxiao.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员和服务接口
 */
@RestController
@RequestMapping("api/memberAndService")
@Api(value = "会员和服务controller", tags = {"会员和服务接口"})
public class MemberAndServiceController {

    @Resource
    private MemberAndServiceService memberAndServiceService;


    @PostMapping("/update")
    @ApiOperation(value = "更新会员和服务信息", notes = "更新会员和服务信息")
    public Map<String, Object> updateMemberAndService(@RequestBody MemberAndService memberAndService, HttpServletRequest request) {
        try {
            memberAndServiceService.saveMemberAndService(memberAndService);
            return ResultTool.genSuccessResult("修改成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "修改失败:" + e.getMessage());
        }
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建会员和服务信息", notes = "创建会员和服务信息")
    public Map<String, Object> saveMemberAndService(@RequestBody MemberAndService memberAndService, HttpServletRequest request) {
        try {
            memberAndServiceService.saveMemberAndService(memberAndService);
            return ResultTool.genSuccessResult("创建成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "创建失败:" + e.getMessage());
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除会员和服务信息", notes = "删除会员和服务信息")
    public Map<String, Object> deleteMemberAndService(@ApiParam(required = true, value = "会员和服务id") @RequestBody String jsonId) {
        Integer id = JsonTool.parseJsonIdStr(jsonId);
        if (id < 0) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "id参数错误");
        }
        MemberAndService memberAndService = memberAndServiceService.findById(id);
        if (null == memberAndService) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "会员和服务不存在");
        }
        memberAndServiceService.deleteById(id);
        return ResultTool.genSuccessResult("删除成功");
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询会员和服务信息", notes = "根据id查询会员和服务信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "会员和服务id") @RequestParam String id,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        try {
            MemberAndService memberAndService = memberAndServiceService.findById(Integer.valueOf(id));
            if (null != memberAndService) {
                result.put("data", memberAndService);
                result.put("message", "查询成功");
                result.put("code", ResultTool.SuccessCode);
            } else {
                result.put("message", "查询失败，没有结果");
                result.put("code", ResultTool.ErrorCode_1);
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.put("message", "查询失败" + e.getMessage());
            result.put("code", ResultTool.ErrorCode_2);
        }

        return result;
    }


    @GetMapping("/list")
    @ApiOperation(value = "查询会员和服务列表", notes = "查询会员和服务列表")
    public Map<String, Object> findList(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        List<MemberAndService> list = memberAndServiceService.findList(param);
        if (null != list && list.size() > 0) {
            result.put("data", list);
            result.put("total", list.size());
            result.put("message", "查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询会员和服务分页列表", notes = "查询会员和服务分页列表")
    public Map<String, Object> findListPage(@ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                            @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        if (null == pageNo) {
            pageNo = 1;
        } else if (pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        param.put("GT_id", 0);
        Page<MemberAndService> list = memberAndServiceService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            result.put("data", list);
            result.put("message", "分页查询成功");
            result.put("code", 10000);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }

        return result;
    }


    @GetMapping("/wxapi/getInfo")
    @ApiOperation(value = "查询会员和服务信息", notes = "查询会员和服务信息")
    public Map<String, Object> getInfo() {

        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("GT_id", 0);
            List<MemberAndService> list = memberAndServiceService.findList(param);
            if (null != list && list.size() > 0) {
                result.put("data", list.get(0));
                result.put("total", list.size());
                result.put("message", "查询成功");
                result.put("code", 10000);
            } else {
                return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
            }

        } catch (Exception e) {
            e.printStackTrace();
            result.put("message", "查询失败" + e.getMessage());
            result.put("code", ResultTool.ErrorCode_2);
        }

        return result;
    }

}
