package com.ff.weijinxiao.controller;


import com.ff.weijinxiao.domain.MemberPackage;
import com.ff.weijinxiao.service.MemberPackageService;
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/memberPackage")
@Api(value = "会员套餐controller", tags = {"会员套餐接口"})
public class MemberPackageController {

    @Resource
    private MemberPackageService memberPackageService;


    @PostMapping("/update")
    @ApiOperation(value = "更新会员套餐信息", notes = "更新会员套餐信息")
    public Map<String, Object> updateMemberPackage(@RequestBody MemberPackage memberPackage, HttpServletRequest request) {
        try {
            memberPackageService.saveMemberPackage(memberPackage);
            return ResultTool.genSuccessResult("修改成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "修改失败:" + e.getMessage());
        }
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建会员套餐信息", notes = "创建会员套餐信息")
    public Map<String, Object> saveMemberPackage(@RequestBody MemberPackage memberPackage, HttpServletRequest request) {
        try {
            memberPackageService.saveMemberPackage(memberPackage);
            return ResultTool.genSuccessResult("创建成功");
        } catch (Exception e) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "创建失败:" + e.getMessage());
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除会员套餐信息", notes = "删除会员套餐信息")
    public Map<String, Object> deleteMemberPackage(@ApiParam(required = true, value = "会员套餐id") @RequestBody String jsonId) {
        Integer id = JsonTool.parseJsonIdStr(jsonId);
        if (id < 0) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "id参数错误");
        }
        MemberPackage memberPackage = memberPackageService.findById(id);
        if (null == memberPackage) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "会员套餐不存在");
        }
        memberPackage.setIsDelete(1);
        memberPackageService.saveMemberPackage(memberPackage);
        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 {
            MemberPackage memberPackage = memberPackageService.findById(Integer.valueOf(id));
            if (null != memberPackage) {
                result.put("data", memberPackage);
                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(@ApiParam(required = false, value = "会员套餐名字") @RequestParam(required = false) String phone,
                                        HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        param.put("EQ_isDelete", 0);
        if (null != phone && phone.length() > 0) {
            param.put("LIKE_phone", phone);
        }
        List<MemberPackage> list = memberPackageService.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 = "会员套餐电话") @RequestParam(required = false) String phone,
                                            @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);
        param.put("EQ_isDelete", 0);
        if (null != phone && phone.length() > 0) {
            param.put("LIKE_phone", phone);
        }
        Page<MemberPackage> list = memberPackageService.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/listpage")
    @ApiOperation(value = "查询会员套餐分页列表", notes = "查询会员套餐分页列表")
    public Map<String, Object> wxfindListPage(@ApiParam(required = false, value = "会员套餐标题") @RequestParam(required = false) String title,
                                              @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                              @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize,
                                              HttpServletRequest request) {

        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);
        param.put("EQ_isDelete", 0);
        if (null != title && title.length() > 0) {
            param.put("LIKE_title", title);
        }

        Page<MemberPackage> list = memberPackageService.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;
    }

}
