package com.huiju.estateagents.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.common.CommonUtils;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaShopType;
import com.huiju.estateagents.service.ITaShopTypeService;
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.huiju.estateagents.service.ITaShopService;
import com.huiju.estateagents.entity.TaShop;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

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

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

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

    @Autowired
    public ITaShopService iTaShopService;

    @Autowired
    public ITaShopTypeService iTaShopTypeService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/{client}/shop",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean getList(@ApiParam("客户端") @PathVariable String client,
                                @ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                @ApiParam("商铺名称") @RequestParam(value = "name", required = false) String name,
                                @ApiParam("商铺分类") @RequestParam(value = "typeId", required = false) String typeId,
                                @ApiParam("排序字段") @RequestParam(value = "sortField", defaultValue = "create_date") String sortField,
                                @ApiParam("首页推荐") @RequestParam(value = "isIndex", required = false) Boolean isIndex,
                                HttpServletRequest request) throws Exception{

        Integer orgId = getOrgId(request);
        IPage<TaShop> pg = new Page<>(pageNum, pageSize);
        QueryWrapper<TaShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.like(!StringUtils.isEmpty(name), "shop_name", name);
        queryWrapper.eq(!StringUtils.isEmpty(typeId), "type_id", typeId);
        queryWrapper.eq(null != isIndex, "is_index", null != isIndex && isIndex ? 1 : 0);
        queryWrapper.gt("status", CommConstant.STATUS_DELETE);
        queryWrapper.orderByDesc(sortField);

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

    /**
     * 保存对象
     * @param taShop 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shop",method= RequestMethod.POST)
    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean taShopAdd(@ApiParam("保存内容") @RequestBody TaShop taShop,
                                  HttpServletRequest request) throws Exception{

        Integer orgId = getOrgId(request);
        taShop.setShopId(CommonUtils.getUUIDS());
        taShop.setOrgId(orgId);

        TaShopType taShopType = iTaShopTypeService.getById(taShop.getTypeId());
        if (null != taShopType) {
            taShop.setTypeName(taShopType.getTypeName());
        }

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

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/shop/{id}", method= RequestMethod.DELETE)
    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
    public ResponseBean taShopDelete(@ApiParam("对象ID") @PathVariable String id,
                                     HttpServletRequest request) throws Exception{

        Integer orgId = getOrgId(request);
        TaShop taShop = iTaShopService.getById(id);
        if (null == taShop || !orgId.equals(taShop.getOrgId())) {
            return ResponseBean.error("当前数据不存在或者无权限", ResponseBean.ERROR_UNAVAILABLE);
        }

        taShop.setStatus(CommConstant.STATUS_DELETE);
        return taShopUpdate(id, taShop);
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taShop 实体对象
     * @return
     */
    @RequestMapping(value="/admin/shop/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean taShopUpdate(@ApiParam("对象ID") @PathVariable String id,
                                     @ApiParam("更新内容") @RequestBody TaShop taShop) throws Exception{

        taShop.setShopId(id);
        TaShopType taShopType = iTaShopTypeService.getById(taShop.getTypeId());
        if (null != taShopType) {
            taShop.setTypeName(taShopType.getTypeName());
        }
        if (iTaShopService.updateById(taShop)){
            return ResponseBean.success(iTaShopService.getById(id));
        }else {
            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/{client}/shop/{id}",method= RequestMethod.GET)
    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taShopGet(@ApiParam("客户端") @PathVariable String client,
                                  @ApiParam("对象ID") @PathVariable String id) throws Exception{
        return ResponseBean.success(iTaShopService.getById(id));
    }
}
