package com.huiju.estateagents.controller;

import com.alibaba.fastjson.JSONObject;
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.bo.request.PresecretRecordRequestBO;
import com.huiju.estateagents.common.DateUtils;
import com.huiju.estateagents.entity.TaPreselectionRecord;
import com.huiju.estateagents.service.ITaPreselectionRecordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
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;
import java.util.List;

/**
 * <p>
 * 预选记录表  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/api")
public class TaPreselectionRecordController extends BaseController {

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

    @Autowired
    public ITaPreselectionRecordService iTaPreselectionRecordService;


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

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

    /**
     * 保存对象
     *
     * @param taPreselectionRecord 实体对象
     * @return
     * @author gushaoyong
     */
    @RequestMapping(value = "/wx/taPreselectionRecord", method = RequestMethod.POST)
    public ResponseBean taPreselectionRecordAdd(@RequestBody TaPreselectionRecord taPreselectionRecord, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        logger.info("taPreselectionRecordAdd 接收参数，taPreselectionRecord：{}", JSONObject.toJSONString(taPreselectionRecord));

        taPreselectionRecord.setOrgId(orgId);
        try {

            // 校验参数
            if (null == taPreselectionRecord.getHouseId() || StringUtils.isBlank(taPreselectionRecord.getPersonId()) ||
                taPreselectionRecord.getOrgId() == null) {
                logger.info("taPreselectionRecordAdd 参数错误");
                responseBean.addError("参数错误");
                return responseBean;
            }

            responseBean = iTaPreselectionRecordService.taPreselectionRecordAdd(taPreselectionRecord);
            logger.info("taPreselectionRecordAdd 返回结果，responseBean：{}", JSONObject.toJSONString(responseBean));

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taPreselectionRecordAdd -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 修改对象
     *
     * @param id                   实体ID
     * @param taPreselectionRecord 实体对象
     * @return
     */
    @RequestMapping(value = "/taPreselectionRecord/{id}", method = RequestMethod.PUT)
    public ResponseBean taPreselectionRecordUpdate(@PathVariable Integer id,
                                                   @RequestBody TaPreselectionRecord taPreselectionRecord,
                                                   HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taPreselectionRecord.setUpdateDate(LocalDateTime.now());
            taPreselectionRecord.setUpdateUser(getUserId(request));
            if (iTaPreselectionRecordService.updateById(taPreselectionRecord)) {
                responseBean.addSuccess(taPreselectionRecord);
            } else {
                responseBean.addError("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taPreselectionRecordUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

    /**
     * 取消房源预选
     *
     * @return
     */
    @RequestMapping(value = "/wx/taPreselectionRecordCancel", method = RequestMethod.DELETE)
    public ResponseBean taPreselectionRecordCancel(@RequestParam("houseId") Integer houseId,
                                                   @RequestParam("personId") String personId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaPreselectionRecordService.taPreselectionRecordCancel(personId, houseId);
        } catch (Exception e) {
            logger.error("taPreselectionRecordCancel -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 获取当前用户预选记录
     *
     * @param buildingId
     * @param salesBatchId
     * @param apartmentId
     * @param houseLockingStatus
     * @param customerLocked
     * @param pageNumber
     * @param pageSize
     * @param personId
     * @param source             来源，raiseChoose 为认筹添加房源来源
     * @param request
     * @return
     */
    @RequestMapping(value = "/wx/listPreselectionRecord", method = RequestMethod.GET)
    public ResponseBean listPreselectionRecord(@RequestParam(value = "buildingId", required = false) String buildingId,
                                               @RequestParam(value = "salesBatchId", required = false) Integer salesBatchId,
                                               @RequestParam(value = "apartmentId", required = false) String apartmentId,
                                               @RequestParam(value = "houseLockingStatus", required = false) String houseLockingStatus,
                                               @RequestParam(value = "customerLocked", required = false) String customerLocked,
                                               @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam("personId") String personId,
                                               @RequestParam(value = "source", required = false) String source,
                                               HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        try {
            responseBean.addSuccess(iTaPreselectionRecordService.listPreselectionRecord(pageNumber, pageSize, orgId, personId, buildingId,
                    salesBatchId, apartmentId, houseLockingStatus, customerLocked, source));
        } catch (Exception e) {
            logger.error("listPreselectionRecord -=- {}", e);
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 查询预选记录列表
     *
     * @param recordRequestBO
     * @param salesBatchId
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/admin/taPreselectRecord/{salesBatchId}", method = RequestMethod.GET)
    public ResponseBean selectPreSelectRecordList(PresecretRecordRequestBO recordRequestBO,
                                                  @PathVariable("salesBatchId") Integer salesBatchId,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recordRequestBO.setSalesBatchId(salesBatchId);
            recordRequestBO.setOrgId(getOrgId(request));
            responseBean = iTaPreselectionRecordService.selectPreSelectRecordList(recordRequestBO, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taHousingResourcesList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 批量删除预选记录
     *
     * @param preselectionRecordList
     * @param request
     * @return
     */
    @RequestMapping(value = "/admin/preselectRecord/batchDelete", method = RequestMethod.PUT)
    public ResponseBean batchDeletePreselectRecord(@RequestBody List<TaPreselectionRecord> preselectionRecordList,
                                                   HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaPreselectionRecordService.batchDeletePreselectRecord(preselectionRecordList, getOrgId(request), getUserId(request));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("batchDeletePreselectRecord -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
