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.entity.TdPointsRules;
import com.huiju.estateagents.service.ITdPointsRulesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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 org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    * 积分获取规则字典表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/api")
@Api(value = "积分", tags = "积分")
public class TdPointsRulesController extends BaseController {

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

    @Autowired
    public ITdPointsRulesService iTdPointsRulesService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "后台积分列表", notes = "后台积分列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "buildingId", paramType = "query",value = "项目id"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "type", paramType = "query",value = "类型")
    })
    @RequestMapping(value="/admin/tdPointsRules",method= RequestMethod.GET)
    public ResponseBean tdPointsRulesList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value ="buildingId", required = false) String buildingId,
                                          @RequestParam(value = "type", required = false) String type,
                                          HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TdPointsRules> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TdPointsRules> queryWrapper = new QueryWrapper<>();
            Integer orgId = getOrgId(request);
            queryWrapper.eq("org_id",orgId);
            queryWrapper.orderByDesc("create_date");
            queryWrapper.eq(StringUtils.isNotBlank(buildingId),"building_id", buildingId);
            if (CommConstant.POINTS_RULE_PLATFORM.equals(type)) {
                queryWrapper.isNull("building_id");
            } else if (CommConstant.POINTS_RULE_PROJECT.equals(type)) {
                queryWrapper.isNotNull("building_id");
            }

            IPage<TdPointsRules> result = iTdPointsRulesService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "微信积分列表", notes = "微信积分列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "一页多少行"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "buildingId", paramType = "query",value = "项目id"),
            @ApiImplicitParam(dataTypeClass = String.class, name = "type", paramType = "query",value = "类型")
    })
    @RequestMapping(value="/wx/tdPointsRules",method= RequestMethod.GET)
    public ResponseBean tdWxPointsRulesList(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNumber,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                            @RequestParam(value ="buildingId", required = false) String buildingId,
                                            @RequestParam(value = "type", required = false) String type,
                                            HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TdPointsRules> pg = new Page<>(pageNumber, pageSize);

            QueryWrapper<TdPointsRules> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");
            queryWrapper.eq(StringUtils.isNotBlank(buildingId),"building_id", buildingId);
            Integer orgId = getOrgId(request);
            queryWrapper.eq(orgId != null,"org_id",orgId);
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            if(null != type){
                if (CommConstant.POINTS_RULE_PLATFORM.equals(type)) {
                    queryWrapper.isNull("building_id");
                } else if (CommConstant.POINTS_RULE_PROJECT.equals(type)) {
                    queryWrapper.isNotNull("building_id");
                }
            }
            IPage<TdPointsRules> result = iTdPointsRulesService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param tdPointsRules 实体对象
     * @return
     */
    @RequestMapping(value="/tdPointsRules",method= RequestMethod.POST)
    public ResponseBean tdPointsRulesAdd(@RequestBody TdPointsRules tdPointsRules){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdPointsRulesService.save(tdPointsRules)){
                responseBean.addSuccess(tdPointsRules);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/tdPointsRules/{id}", method= RequestMethod.DELETE)
    public ResponseBean tdPointsRulesDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTdPointsRulesService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param tdPointsRules 实体对象
     * @return
     */
    @ApiOperation(value = "后台积分规则修改", notes = "后台积分规则修改")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "TdPointsRules", name = "tdPointsRules", paramType = "body",value = "积分详细数据")
    })
    @RequestMapping(value="/admin/tdPointsRules",method= RequestMethod.PUT)
    public ResponseBean tdPointsRulesUpdate(@RequestBody TdPointsRules tdPointsRules){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTdPointsRulesService.updateById(tdPointsRules)){
                responseBean.addSuccess(tdPointsRules);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    
    /**
     * 停用启用
     * @param tdPointsRules 实体对象
     * @return
     */
    @ApiOperation(value = "后台积分规则停用启用", notes = "后台积分规则停用启用")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "TdPointsRules", name = "tdPointsRules", paramType = "body",value = "积分详细数据")
    })
    @RequestMapping(value="/admin/tdPointsRules/change",method= RequestMethod.PUT)
    public ResponseBean tdPointsRulesChange(@RequestBody TdPointsRules tdPointsRules){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(null != tdPointsRules.getStatus() && tdPointsRules.getStatus().equals(CommConstant.STATUS_NORMAL)){
                tdPointsRules.setStatus(CommConstant.STATUS_UNACCALIMED);
            }else {
                tdPointsRules.setStatus(CommConstant.STATUS_NORMAL);
            }
            if (iTdPointsRulesService.updateById(tdPointsRules)){
                responseBean.addSuccess(tdPointsRules);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "后台积分规则详情", notes = "后台积分规则详情")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", paramType = "path",value = "积分id")
    })
    @RequestMapping(value="/admin/tdPointsRules/{id}",method= RequestMethod.GET)
    public ResponseBean tdPointsRulesGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTdPointsRulesService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tdPointsRulesDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
