package com.yunzhi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.entity.TaShopBanner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.yunzhi.marketing.service.ITaShopBannerService;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    * 商户Banner  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-11-12
 */

@Api(tags = "商户Banner ")
@RestController
@RequestMapping("/api")
public class TaShopBannerController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaShopBannerController.class);

    @Autowired
    public ITaShopBannerService iTaShopBannerService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/{client}/shop-banner",method= RequestMethod.GET)
    @ApiOperation(value="商铺Banner列表", notes = "商铺Banner列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taShopBannerList(@ApiParam(value = "客户端", allowableValues = "wx,admin") @PathVariable String client,
                                         @ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{

		    IPage<TaShopBanner> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaShopBanner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("sort_no");

            IPage<TaShopBanner> result = iTaShopBannerService.page(pg, queryWrapper);
            return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param taShopBanner 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shop-banner",method= RequestMethod.POST)
    @ApiOperation(value="商铺Banner保存", notes = "商铺Banner保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean taShopBannerAdd(@ApiParam("保存内容") @RequestBody TaShopBanner taShopBanner,
                                        HttpServletRequest request) throws Exception{

        Integer orgId = getOrgId(request);
        taShopBanner.setOrgId(orgId);

        if (iTaShopBannerService.save(taShopBanner)){
            return ResponseBean.success(taShopBanner);
        }else {
            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/shop-banner/{id}", method= RequestMethod.DELETE)
    @ApiOperation(value="商铺Banner删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
    public ResponseBean taShopBannerDelete(@ApiParam("对象ID") @PathVariable Integer id, HttpServletRequest request) throws Exception{
        TaShopBanner taShopBanner = iTaShopBannerService.getById(id);
        if (null == taShopBanner || CommConstant.STATUS_DELETE.equals(taShopBanner.getStatus())) {
            return ResponseBean.error("未找到需要删除的内容, 请重试", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        taShopBanner.setStatus(CommConstant.STATUS_DELETE);
        return taShopBannerUpdate(id, taShopBanner, request);
    }
//
    /**
     * 修改对象
     * @param id  实体ID
     * @param taShopBanner 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shop-banner/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="商铺Banner更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean taShopBannerUpdate(@ApiParam("对象ID") @PathVariable Integer id,
                                           @ApiParam("更新内容") @RequestBody TaShopBanner taShopBanner,
                                           HttpServletRequest request) throws Exception{
        Integer orgId = getOrgId(request);
        if (!orgId.equals(taShopBanner.getOrgId())) {
            return ResponseBean.error("暂无权修改当前数据", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        taShopBanner.setSerialNo(id);
        if (iTaShopBannerService.updateById(taShopBanner)){
            return ResponseBean.success(iTaShopBannerService.getById(id));
        }else {
            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/shop-banner/{id}",method= RequestMethod.GET)
    @ApiOperation(value="商铺Banner详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taShopBannerGet(@ApiParam("对象ID") @PathVariable Integer id, HttpServletRequest request) throws Exception{
        TaShopBanner taShopBanner = iTaShopBannerService.getById(id);
        if (null == taShopBanner || CommConstant.STATUS_DELETE.equals(taShopBanner.getStatus())) {
            return ResponseBean.error("未找到需要删除的内容, 请重试", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        if (!orgId.equals(taShopBanner.getOrgId())) {
            return ResponseBean.error("暂无权修改当前数据", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        return ResponseBean.success(taShopBanner);
    }
}
