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.TaBuildingApartment;
import com.huiju.estateagents.entity.TaBuildingPanorama;
import com.huiju.estateagents.service.ITaBuildingApartmentService;
import com.huiju.estateagents.service.ITaBuildingPanoramaService;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-01-30
 */
@RestController
@RequestMapping("/api")
public class TaBuildingPanoramaController extends BaseController {

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

    @Autowired
    public ITaBuildingPanoramaService iTaBuildingPanoramaService;


    @Autowired
    private ITaBuildingApartmentService taBuildingApartmentService;

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value="/admin/panorama/{buildingId}",method= RequestMethod.GET)
    public ResponseBean taBuildingPanoramaList(@PathVariable String buildingId,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            QueryWrapper<TaBuildingPanorama> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",getOrgId(request));
            queryWrapper.eq("building_id",buildingId);
            queryWrapper.eq("status",CommConstant.STATUS_NORMAL);
            
            queryWrapper.orderByDesc("create_date");
            List<TaBuildingPanorama> result = iTaBuildingPanoramaService.list(queryWrapper);

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


    /**
     * 获取全景图的户型列表
     * @return
     */
    @RequestMapping(value="/admin/panorama/apartment",method= RequestMethod.GET)
    public ResponseBean getApartmentList(@RequestParam(value ="buildingId") String buildingId,
                                               HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //获取此楼盘下的所有的户型
            QueryWrapper<TaBuildingApartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",CommConstant.STATUS_NORMAL);
            queryWrapper.eq("apartment_type",CommConstant.APARTMENT_TYPR_APART);
            queryWrapper.eq("building_id",buildingId);
            List<TaBuildingApartment> apartmentList = taBuildingApartmentService.list(queryWrapper);

            //获取此楼盘下所有已经有全景图的户型
            QueryWrapper<TaBuildingPanorama> panoramaQueryWrapper = new QueryWrapper<>();
            panoramaQueryWrapper.eq("status",CommConstant.STATUS_NORMAL);
            panoramaQueryWrapper.eq("panorama_type",CommConstant.PANORAMA_TYPE_APARTMENT);
            panoramaQueryWrapper.eq("building_id",buildingId);
            panoramaQueryWrapper.eq("org_id",getOrgId(request));
            List<TaBuildingPanorama> panoramaList = iTaBuildingPanoramaService.list(panoramaQueryWrapper);
            List<String> stringList = panoramaList.stream().map(TaBuildingPanorama::getApartmentId).collect(Collectors.toList());

            List<TaBuildingApartment> resultList = new ArrayList<>();
            //过滤已经选择的
            apartmentList.forEach(e -> {
                if (!stringList.contains(e.getApartmentId())){
                    resultList.add(e);
                }
            });

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

    /**
     * 保存对象
     * @param taBuildingPanorama 实体对象
     * @return
     */
    @RequestMapping(value="/admin/panorama",method= RequestMethod.POST)
    public ResponseBean taBuildingPanoramaAdd(@RequestBody TaBuildingPanorama taBuildingPanorama,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taBuildingPanorama.setStatus(CommConstant.STATUS_NORMAL);
            taBuildingPanorama.setCreateDate(LocalDateTime.now());
            taBuildingPanorama.setOrgId(getOrgId(request));
            //把户型给内容
            if (taBuildingPanorama.getPanoramaType().equals(CommConstant.PANORAMA_TYPE_APARTMENT)){
                TaBuildingApartment apartment = taBuildingApartmentService.getById(taBuildingPanorama.getApartmentId());
                taBuildingPanorama.setContent(apartment.getApartmentName());
            }
            if (iTaBuildingPanoramaService.save(taBuildingPanorama)){
                responseBean.addSuccess(taBuildingPanorama);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingPanoramaAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象--逻辑删除
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/admin/panorama/{id}", method= RequestMethod.DELETE)
    public ResponseBean taBuildingPanoramaDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            TaBuildingPanorama taBuildingPanorama = new TaBuildingPanorama();
            taBuildingPanorama.setPanoramaId(id);
            taBuildingPanorama.setStatus(CommConstant.STATUS_UNACCALIMED);
            if(iTaBuildingPanoramaService.updateById(taBuildingPanorama)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingPanoramaDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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