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.entity.TaRaiseHouse;
import com.yunzhi.marketing.service.ITaRaiseHouseService;
import com.yunzhi.marketing.service.ITaRaiseRecordService;
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 java.time.LocalDateTime;

/**
 * <p>
 * 认筹单与房源关系表  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/api")
public class TaRaiseHouseController extends BaseController {

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

    @Autowired
    public ITaRaiseHouseService iTaRaiseHouseService;

    @Autowired
    private ITaRaiseRecordService iTaRaiseRecordService;


    /**
     * 分页查询列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/taRaiseHouse", method = RequestMethod.GET)
    public ResponseBean taRaiseHouseList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaRaiseHouse> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaRaiseHouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

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

    /**
     * 保存对象
     *
     * @param taRaiseHouse 实体对象
     * @return
     */
    @RequestMapping(value = "/admin/taRaiseHouse", method = RequestMethod.POST)
    public ResponseBean taRaiseHouseAdd(TaRaiseHouse taRaiseHouse, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {

            // 校验参数
            if (taRaiseHouse.getRaiseRecordId() == null) {
                responseBean.addError("参数错误");
                return responseBean;
            }

            Integer orgId = getOrgId(request);
            Integer userId = getUserId(request);

            // 认筹表房源数+1
            iTaRaiseRecordService.addHouseNum(taRaiseHouse.getRaiseRecordId(), orgId, userId);

            taRaiseHouse.setOrgId(orgId);
            taRaiseHouse.setCreateDate(LocalDateTime.now());
            if (iTaRaiseHouseService.save(taRaiseHouse)) {
                responseBean.addSuccess(taRaiseHouse);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRaiseHouseAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 修改对象
     *
     * @param id           实体ID
     * @param taRaiseHouse 实体对象
     * @return
     */
    @RequestMapping(value = "/taRaiseHouse/{id}", method = RequestMethod.PUT)
    public ResponseBean taRaiseHouseUpdate(@PathVariable Integer id,
                                           @RequestBody TaRaiseHouse taRaiseHouse) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaRaiseHouseService.updateById(taRaiseHouse)) {
                responseBean.addSuccess(taRaiseHouse);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRaiseHouseUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/taRaiseHouse/{id}", method = RequestMethod.GET)
    public ResponseBean taRaiseHouseGet(@PathVariable Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaRaiseHouseService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRaiseHouseDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
