package org.jeecg.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.api.bean.CommonHomeBo;
import org.jeecg.api.bean.HousePageList;
import org.jeecg.api.service.ApiIndexService;
import org.jeecg.api.untils.HttpConf;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.commCity.entity.CommCity;
import org.jeecg.modules.commCity.service.ICommCityService;
import org.jeecg.modules.commonAccount.entity.CommonAccount;
import org.jeecg.modules.commonAccount.service.ICommonAccountService;
import org.jeecg.modules.commonClass.entity.CommonClass;
import org.jeecg.modules.commonClass.service.ICommonClassService;
import org.jeecg.modules.commonComment.entity.CommonComment;
import org.jeecg.modules.commonComment.service.ICommonCommentService;
import org.jeecg.modules.commonHome.entity.CommonHome;
import org.jeecg.modules.commonHome.service.ICommonHomeService;
import org.jeecg.modules.commonIconImage.entity.CommonIconImage;
import org.jeecg.modules.commonIconImage.service.ICommonIconImageService;
import org.jeecg.modules.commonNews.entity.CommonNews;
import org.jeecg.modules.commonNews.service.ICommonNewsService;
import org.jeecg.modules.commonNotice.entity.CommonNotice;
import org.jeecg.modules.commonNotice.service.ICommonNoticeService;
import org.jeecg.modules.commonPrice.entity.CommonPrice;
import org.jeecg.modules.commonPrice.service.ICommonPriceService;
import org.jeecg.modules.commonYear.entity.CommonYear;
import org.jeecg.modules.commonYear.service.ICommonYearService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ApiIndexServiceImpl implements ApiIndexService {

    @Resource
    private IHanHaiMemberService memberService;
    @Resource
    private ICommonNoticeService noticeService;
    @Resource
    private ICommonClassService classService;
    @Resource
    private ICommonHomeService homeService;
    //权限配置
    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private ICommonIconImageService iconImageService;
    @Resource
    private ICommonNewsService newsService;

    @Resource
    private ICommonCommentService commentService;


    @Resource
    private ICommonPriceService priceService;
    @Resource
    private ICommonYearService yearService;
    @Resource
    private ICommCityService cityService;
    @Resource
    private ICommonAccountService accountService;




    @Resource
    private RedisUtil redisUtil;
    @Resource
    private HttpConf httpConf;


    //获取通知公告
    @Override
    public Result<?> getNotice(){
        CommonNotice commonNotice = noticeService.lambdaQuery()
                .orderByDesc(CommonNotice::getCreateTime)
                .list().get(0);
        return Result.OK(commonNotice);
    }


    //获取租房分类类型
    @Override
    public Result<?> getHouseType(){
        List<CommonClass> list = classService.lambdaQuery().list();
        return Result.OK(list);
    }


    //房源搜索条件-类型
    @Override
    public Result<?> getHouseIconClass(){
        List<CommonClass> list = classService.lambdaQuery().list();
        return Result.OK(list);
    }

    //房源搜索条件-价格
    @Override
    public Result<?> getHousePrice(){
        List<CommonPrice> list = priceService.list();
        return Result.OK(list);
    }

    //房源搜索条件-区域
    @Override
    public Result<?> getHouseArea(){
        List<CommCity> list = cityService.list();
        return Result.OK(list);
    }

    //房源搜索条件-年限
    @Override
    public Result<?> getHouseYear(){
        List<CommonYear> list = yearService.list();
        return Result.OK(list);
    }


    @Override
    public Result<?> getAccountCode(String token){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //角色不是经纪人就报错
        if(hanHaiMember.getRole()!=2){
            return Result.error("请先成为经纪人");
        }
        CommonAccount one = accountService.lambdaQuery()
                .eq(CommonAccount::getUserId, hanHaiMember.getId())
                .one();
        return Result.OK(one);
    }


    //根据租房分类信息获取租房带分页列表信息
    @Override
    public Result<?> getHousePageList(HousePageList pageBean,
//                                      String price,
//                                      String  areaId,
//                                      String year,
//                                      String typeId,
                                      CommonHomeBo bo,
                                      HttpServletRequest req){

        CommonHome commonHome = new CommonHome();
        QueryWrapper<CommonHome> queryWrapper = QueryGenerator.initQueryWrapper(commonHome, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");

        if(StringUtils.isNotBlank(pageBean.getTitle())){
            //标题包含pageBean.getTitle()

            // 包含标题或者地址
            queryWrapper.and(qw -> {
                qw.like("title", pageBean.getTitle())
                .or()
                .like("address", pageBean.getTitle());
            });
        }

        if(StringUtils.isNotBlank(bo.getPriceId())){
            //价格小于price
            queryWrapper.le("price",bo.getPriceId());
        }
        if(StringUtils.isNotBlank(bo.getAreaId())){
            //地点区域输入areaId
            queryWrapper.eq("area_id",bo.getAreaId());
        }
        if(StringUtils.isNotBlank(bo.getYearId())){
            //年限小于或者等于year
            queryWrapper.le("time_go",bo.getYearId());
        }
        if(StringUtils.isNotBlank(bo.getTypeId())){
            //租房分类
            queryWrapper.eq("common_class",bo.getTypeId());
        }

        // 分类
        queryWrapper.eq(StringUtils.isNotBlank(bo.getClassId()), "class_id", bo.getClassId());

        // 种类
        queryWrapper.eq(StringUtils.isNotBlank(bo.getCommonClass()), "common_class", bo.getCommonClass());

        // 审核状态通过的
        queryWrapper.eq("status", 1);

        Page<CommonHome> page = new Page<CommonHome>(pageBean.getPageNo(), pageBean.getPageSize());
        IPage<CommonHome> pageList = homeService.page(page, queryWrapper);







//        if(pageBean.getClassId()!=null){
//            page1 = homeService
//                    .lambdaQuery()
//                    .eq(CommonHome::getClassId,pageBean.getClassId())
//                    .orderByDesc(CommonHome::getCreateTime)
//                    .page(page);
//        }
//
//        //如果区域标识不为空
//        if(StringUtils.isNotBlank(pageBean.getIconClass())){
//            page1 = homeService
//                    .lambdaQuery()
//                    .eq(CommonHome::getIconClass,pageBean.getIconClass())
//                    .orderByDesc(CommonHome::getCreateTime)
//                    .page(page);
//
//        }











        return Result.OK(pageList);
    }



    @Override
    public Result<?> getHouseDetail(String houseId){
        CommonHome commonHome = homeService.getById(houseId);
        return Result.OK(commonHome);
    }


    //查询我发布的房源列表带分页
    @Override
    public Result<?> getMyHousePageList(String token,HousePageList pageBean){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        Page<CommonHome> page = new Page<CommonHome>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<CommonHome> page1 = homeService
                .lambdaQuery()
                .eq(CommonHome::getUserId,hanHaiMember.getId())
                .orderByDesc(CommonHome::getCreateTime)
                .page(page);

        return Result.OK(page1);
    }




    //发布或编辑房源
    @Override
    public Result<?> saveOrUpdateHouse(String token, CommonHome home){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        home.setUserId(hanHaiMember.getId());
        homeService.saveOrUpdate(home);
        return Result.OK("发布成功");
    }

    //删除房源信息
    @Override
    public Result<?> deleteHouse(String token,String houseId) {
        homeService.removeById(houseId);
        return Result.OK("删除成功");
    }


    //经济人账号密码登录
    @Override
    public Result<?> login(String token,String username, String password) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //根据输入账号密码查询账号密码是否正确
        CommonAccount account = accountService.lambdaQuery()
                .eq(CommonAccount::getAccount, username)
                .eq(CommonAccount::getPassword, password)
                .one();
        if(account==null){
            return Result.error("账号或密码错误");
        }
        return Result.OK("经济人登录成功");
    }


    //房东输入邀请码登录
    @Override
    public Result<?> loginByInviteCode(String token,String inviteCode) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        CommonAccount account = accountService.lambdaQuery()
                .eq(CommonAccount::getInvoice, inviteCode)
                .one();
        //邀请码错误
        if(account==null){
            return Result.error("邀请码错误");
        }
        return Result.OK("房东登录成功");
    }



    //获取首页图标的接口
    @Override
    public Result<?> getIndexIcon(){
        List<CommonIconImage> list = iconImageService.lambdaQuery().list();
        return Result.OK(list);
    }



    //获取发现列表页面数据带分页
    @Override
    public Result<?> getFindPageList(HousePageList pageBean){
        Page<CommonNews> page = new Page<CommonNews>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<CommonNews> page1 = newsService
                .lambdaQuery()
                .orderByDesc(CommonNews::getCreateTime)
                .page(page);
        return Result.OK(page1);
    }

    //发布或编辑发现
    @Override
    public Result<?> saveOrUpdateFind(String token, CommonNews news){
        newsService.saveOrUpdate(news);
        return Result.OK("发布成功");
    }


    //获取发现列表详情
    @Override
    public Result<?> getFindDetail(String houseId){
        CommonNews byId = newsService.getById(houseId);
        return Result.OK(byId);
    }





    //根据房源标识查询房源的评论列表带分页
    @Override
    public Result<?> getCommentPageList(String houseId,HousePageList bean){
        Page<CommonComment> page = new Page<CommonComment>(bean.getPageNo(), bean.getPageSize());
        Page<CommonComment> page1 = commentService
                .lambdaQuery()
                .eq(CommonComment::getHomeId,houseId)
                .orderByDesc(CommonComment::getCreateTime)
                .page(page);
        return Result.OK(page1);

    }


    //发表评论
    @Override
    public Result<?> saveComment(String token, CommonComment comment){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //评论的房子标识不能为空
        if(StringUtils.isBlank(comment.getHomeId())){
            return Result.error("房源标识不能为空");
        }
        //房源分数不能为空
        if(StringUtils.isBlank(comment.getNum())){
            return Result.error("房源分数不能为空");
        }
        comment.setUserId(hanHaiMember.getId());
        comment.setUserHead(hanHaiMember.getHeadImage());
        comment.setUserName(hanHaiMember.getNickName());
        comment.setCreateTime(new Date());
        if(StringUtils.isBlank(comment.getUserImage())){
            return Result.error("评论用户头像不能为空");
        }
        if(StringUtils.isBlank(comment.getUserValue())){
            return Result.error("评论内容不能为空");
        }
        commentService.save(comment);
        return Result.OK("评论成功");
    }


    //删除评论
    @Override
    public Result<?> deleteComment(String token,String commentId){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        if(!hanHaiMember.getId().equals(commentService.getById(commentId).getUserId())){
            return Result.error("您没有权限删除该评论");
        }
        commentService.removeById(commentId);
        return Result.OK("删除成功");
    }


}
