package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.dto.StoreWechatDto;
import com.ssm.manage.entity.StoreWechat;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.StoreWechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * (StoreWechat)表控制层
 *
 * @author makejava
 * @since 2022-07-07 15:49:57
 */
@RestController
@RequestMapping("storeWechat")
@Api(tags = "门店公众号信息")
@Slf4j
public class StoreWechatController {
    /**
     * 服务对象
     */
    @Autowired
    private StoreWechatService storeWechatService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
//    @RequiresPermissions("system:storeWechat:query")
    public PageResponse get(Serializable id) {
//        return this.storeWechatService.queryById(id);
        PageResponse<StoreWechat> pageResponse = new PageResponse<>();
        ArrayList<StoreWechat> platformCoupons = new ArrayList<>();
        try {
            StoreWechat storeWechat = storeWechatService.queryById(id);
            platformCoupons.add(storeWechat);
        } catch (Exception e) {
            log.error("storeWechat.id", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(platformCoupons);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;

    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//    @RequiresPermissions("system:storeWechat:query")
    public PageResponse listPage(StoreWechatDto dto) {
        PageResponse<StoreWechat> pageResponse = new PageResponse<>();
        PageInfo<StoreWechat> pageInfo = new PageInfo<>();
        try {
            pageInfo = storeWechatService.queryByPage(dto);
        } catch (Exception e) {
            log.error("storeWechat.page", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;

    }

    /**
     * 新增
     *
     * @param storeWechat
     * @return
     */
    @PostMapping("add")
    @ApiOperation("新增")
//    @RequiresPermissions("system:storeWechat:add")
    public GenericResponse add(@RequestBody StoreWechat storeWechat,HttpSession session) {
        GenericResponse<StoreWechat> genericResponse = new GenericResponse<>();
        String key = SessionHelp.getLoginAccount(session);
        Long storeId = SessionHelp.getStoreId(session);
        int i = 0;
        try {
            i = storeWechatService.add(storeWechat, key);
        } catch (Exception e) {
            log.error("storeWechat.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    /**
     * 编辑
     *
     * @param storeWechat
     * @return
     */
    @PutMapping
    @ApiOperation("编辑")
//    @RequiresPermissions("system:storeWechat:edit")
    public GenericResponse edit(@RequestBody StoreWechat storeWechat,HttpSession session) {
        GenericResponse<StoreWechat> genericResponse = new GenericResponse<>();
        String key = SessionHelp.getLoginAccount(session);
        Long storeId = SessionHelp.getStoreId(session);
        int i = 0;
        try {
            i = storeWechatService.edit(storeWechat, key);
        } catch (Exception e) {
            log.error("storeWechat.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除")
//    @RequiresPermissions("system:storeWechat:delete")
    public GenericResponse delete(@PathVariable Serializable id) {
        GenericResponse<StoreWechat> genericResponse = new GenericResponse<>();
        int i = 0;
        try {
            i = storeWechatService.delete(id);
        } catch (Exception e) {
            log.error("storeWechat.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}