package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.support.util.AuthUtil;
import com.propertyManagement.common.enums.RepairsState;
import com.propertyManagement.common.utils.StringUtils;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.propertyManagement.business.domain.bo.WxCommentBo;
import com.propertyManagement.business.domain.vo.WxCommentVo;
import com.propertyManagement.business.domain.WxComment;
import com.propertyManagement.business.mapper.WxCommentMapper;
import com.propertyManagement.business.service.IWxCommentService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 表扬与投诉Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxCommentServiceImpl implements IWxCommentService {

    private final WxCommentMapper baseMapper;

    /**
     * 查询表扬与投诉
     */
    @Override
    public WxCommentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询表扬与投诉
     */
    @Override
    public Map<Integer, Long> userCountAll() {
        Map<Integer, Long> map = new LinkedHashMap<>();
        LoginWxUser wxUser = AuthUtil.getWxUser();
        LambdaQueryWrapper<WxComment> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
            .eq(WxComment::getUserId, wxUser.getId())
            .eq(WxComment::getCommunityId, wxUser.getCommunityId())
            .eq(WxComment::getState, 1);
        map.put(1, baseMapper.selectCount(lambdaQueryWrapper));
        lambdaQueryWrapper.clear();
        lambdaQueryWrapper
            .eq(WxComment::getUserId, wxUser.getId())
            .eq(WxComment::getCommunityId, wxUser.getCommunityId())
            .eq(WxComment::getState, 2);
        map.put(2, baseMapper.selectCount(lambdaQueryWrapper));
        lambdaQueryWrapper.clear();
        lambdaQueryWrapper
            .eq(WxComment::getUserId, wxUser.getId())
            .eq(WxComment::getCommunityId, wxUser.getCommunityId())
            .eq(WxComment::getState, 3);
        map.put(3, baseMapper.selectCount(lambdaQueryWrapper));
        return map;
    }

    /**
     * 查询表扬与投诉列表
     */
    @Override
    public TableDataInfo<WxCommentVo> queryPageList(WxCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxComment> lqw = buildQueryWrapper(bo);
        Page<WxCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询表扬与投诉列表
     */
    @Override
    public List<WxCommentVo> queryList(WxCommentBo bo) {
        LambdaQueryWrapper<WxComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxComment> buildQueryWrapper(WxCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WxComment::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), WxComment::getUserName, bo.getUserName());
        lqw.eq(bo.getCommunityId() != null, WxComment::getCommunityId, bo.getCommunityId());
        lqw.like(bo.getCommunityName() != null, WxComment::getCommunityName, bo.getCommunityName());
        lqw.eq(bo.getState() != null, WxComment::getState, bo.getState());
        lqw.eq(bo.getType() != null, WxComment::getType, bo.getType());
        lqw.eq(bo.getDisposeUserId() != null, WxComment::getDisposeUserId, bo.getDisposeUserId());
        lqw.like(StringUtils.isNotBlank(bo.getDisposeUserName()), WxComment::getDisposeUserName, bo.getDisposeUserName());
        lqw.eq(bo.getCompleteTime() != null, WxComment::getCompleteTime, bo.getCompleteTime());
        lqw.eq(bo.getCloseTime() != null, WxComment::getCloseTime, bo.getCloseTime());
        return lqw;
    }

    @Override
    public Boolean add(WxCommentBo bo) {
        WxComment add = BeanUtil.toBean(bo, WxComment.class);
        validEntityBeforeSave(add);
        LoginWxUser wxUser = AuthUtil.getWxUser();
        add.setUserId(wxUser.getId());
        add.setUserName(wxUser.getName());
        add.setCommunityId(wxUser.getCommunityId());
        add.setCommunityName(wxUser.getCommunityName());
        add.setRoom(wxUser.getRoom());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 新增表扬与投诉
     */
    @Override
    public Boolean insertByBo(WxCommentBo bo) {
        WxComment add = BeanUtil.toBean(bo, WxComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改表扬与投诉
     */
    @Override
    public Boolean updateByBo(WxCommentBo bo) {
        WxComment update = BeanUtil.toBean(bo, WxComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WxComment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除表扬与投诉
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
