package com.iformall.controller.basic;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxShop;
import com.iformall.service.WxShopService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxShop")
public class WxShopController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxShopService wxShopService;

    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "店铺管理-列表")
    public ResultData list(@ModelAttribute WxShop wxShop, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxShopController::list");
        if (null == wxShop){
            wxShop = new WxShop();
        }
        wxShop.setTenantId(getTenantId());
        wxShop.setSortColumns(BaseEntity.SortField.CreateDate_DESC,BaseEntity.SortField.Id_DESC);

        final PageInfo<Map<String, Object>> page = wxShopService.listMapAsPage(wxShop, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "店铺管理-新增")
    public ResultData add(@RequestBody WxShop wxShop) {
        logger.debug("[" + getIpAddr() + "] WxShopController::add");
        wxShop.setTenantId(getTenantId());
        return wxShopService.saveOrUpdate(wxShop);
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    @SystemControllerLog(description = "店铺管理-更新")
    public ResultData update(@RequestBody WxShop wxShop) {
        logger.debug("[" + getIpAddr() + "] WxShopController::update");
        return wxShopService.saveOrUpdate(wxShop);
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @SystemControllerLog(description = "店铺管理-删除")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxShopController::delete");
        Integer isAdmin = getUser().getIsAdmin();
        return wxShopService.deleteById(id, isAdmin);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "店铺管理-id查询")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxShopController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxShopService.getById(id));
    }

    @ApiOperation("获取商铺数据")
    @GetMapping("getShopListByShopNumber")
    @ApiImplicitParam(name = "shopNumber", value = "shopNumber", dataType = "String", paramType = "query", required = true)
    @SystemControllerLog(description = "店铺管理-获取商铺数据")
    public ResultData getbshoplist(String shopNumber) {
        logger.debug("[" + getIpAddr() + "] WxShopController::getbshoplist");
        return wxShopService.getbshoplist(getTenantId(), shopNumber);
    }

    @ApiOperation("获取商户商铺数据")
    @GetMapping("getMerchantShopByShopId")
    @ApiImplicitParam(name = "shopId", value = "shopId", dataType = "String", paramType = "query", required = true)
    @SystemControllerLog(description = "店铺管理-获取商户商铺数据")
    public ResultData getMerchantShopByShopId(String shopId) {
        logger.debug("[" + getIpAddr() + "] WxShopController::getMerchantShopByShopId");
        return wxShopService.getMerchantShopByShopId(getTenantId(), shopId);
    }

    @ApiOperation("查询商铺号是否存在")
    @GetMapping("hasShopNumber")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopNumber", value = "shopNumber", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "type", dataType = "Integer", paramType = "query", required = true)})
    @SystemControllerLog(description = "店铺管理-查询商铺号是否存在")
    public ResultData hasShopNumber(String shopNumber, Long id, Integer type) {
        logger.debug("[" + getIpAddr() + "] WxShopController::hasShopNumber");
        return wxShopService.hasShopNumber(getTenantId(), shopNumber, id, type);
    }


    @ApiOperation("分页列表接品-合同访问")
    @GetMapping("listShopFromContract")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "店铺管理-合同访问")
    public ResultData listShopFromContract(@ModelAttribute WxShop wxShop, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxShopController::listShopFromContract");
        if (null == wxShop){
            wxShop = new WxShop();
        }
        wxShop.setTenantId(getTenantId());
        wxShop.setSortColumns(BaseEntity.SortField.CreateDate_DESC,BaseEntity.SortField.Id_DESC);
        final PageInfo<Map<String, Object>> page = wxShopService.listShopFromContract(wxShop, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("导出店铺")
    @GetMapping("/exportShop")
    @SystemControllerLog(description = "店铺管理-导出店铺")
    public void exportShop(@ModelAttribute WxShop wxShop, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxShopController::exportShop");
        if (null == wxShop){
            wxShop = new WxShop();
        }
        wxShop.setTenantId(getTenantId());
        wxShop.setSortColumns(BaseEntity.SortField.CreateDate_DESC,BaseEntity.SortField.Id_DESC);
        wxShopService.exportShop(wxShop, request, response);
    }

    @ApiOperation("导出未出租店铺")
    @GetMapping("/exportNotRentShop")
    @SystemControllerLog(description = "商铺出租数据-未出租商铺列表-导出")
    public void exportNotRentShop(@ModelAttribute WxShop wxShop, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxShopController::exportNotRentShop");
        if (null == wxShop){
            wxShop = new WxShop();
        }
        wxShop.setTenantId(getTenantId());
        wxShop.setSortColumns(BaseEntity.SortField.CreateDate_DESC,BaseEntity.SortField.Id_DESC);
        wxShopService.exportNotRentShop(wxShop, request, response);
    }

    @ApiOperation("未出租商铺分页列表接口")
    @GetMapping("notRentShopList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "未出租商铺分页列表接口")
    public ResultData notRentShopList(@ModelAttribute WxShop wxShop, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxShopController::notRentShopList");
        if (null == wxShop){
            wxShop = new WxShop();
        }
        wxShop.setTenantId(getTenantId());
        wxShop.setSortColumns(BaseEntity.SortField.CreateDate_DESC,BaseEntity.SortField.Id_DESC);

        final PageInfo<Map<String, Object>> page = wxShopService.notRentListMapAsPage(wxShop, pageNum, pageSize);
        return new ResultData(page);
    }
}
