package com.huiju.estateagents.property.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 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.property.service.ITpCommunityService;
import com.huiju.estateagents.property.entity.TpCommunity;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2021-01-19
 */

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

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

    @Autowired
    public ITpCommunityService iTpCommunityService;


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

		    IPage<TpCommunity> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TpCommunity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", getOrgId(request));
            queryWrapper.eq("wx".equals(client), "status", CommConstant.STATUS_NORMAL);
            queryWrapper.gt("admin".equals(client), "status", CommConstant.STATUS_DELETE);
            queryWrapper.orderByDesc("create_date");

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

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

        tpCommunity.setOrgId(getOrgId(request));

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

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

        TpCommunity tpCommunity = iTpCommunityService.getById(id);
        if (null == tpCommunity || CommConstant.STATUS_DELETE.equals(tpCommunity.getStatus())) {
            return ResponseBean.error("没有找到对应的社区信息", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        if (!orgId.equals(tpCommunity.getOrgId())) {
            return ResponseBean.error("暂无操作权限", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        tpCommunity.setStatus(CommConstant.STATUS_DELETE);

        return tpCommunityUpdate(id, tpCommunity, request);
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param tpCommunity 实体对象
     * @return
     */
    @RequestMapping(value="/admin/tpCommunity/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean tpCommunityUpdate(@ApiParam("对象ID") @PathVariable Integer id,
                                          @ApiParam("更新内容") @RequestBody TpCommunity tpCommunity,
                                          HttpServletRequest request) throws Exception{
        tpCommunity.setId(id);

        Integer orgId = getOrgId(request);
        if (!orgId.equals(tpCommunity.getOrgId())) {
            return ResponseBean.error("暂无操作权限", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

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

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/{client}/tpCommunity/{id}",method= RequestMethod.GET)
    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean tpCommunityGet(@ApiParam(value = "客户端", allowableValues = "wx,admin") @PathVariable("client") String client,
                                       @ApiParam("对象ID") @PathVariable Integer id,
                                       HttpServletRequest request) throws Exception{

        TpCommunity tpCommunity = iTpCommunityService.getById(id);
        if (null == tpCommunity || CommConstant.STATUS_DELETE.equals(tpCommunity.getStatus())) {
            return ResponseBean.error("没有找到对应的社区信息", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        if (!orgId.equals(tpCommunity.getOrgId())) {
            return ResponseBean.error("暂无操作权限", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        return ResponseBean.success(tpCommunity);
    }
}
