package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxCommunity;
import com.propertyManagement.business.mapper.WxCommunityMapper;
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.WxAccountBo;
import com.propertyManagement.business.domain.vo.WxAccountVo;
import com.propertyManagement.business.domain.WxAccount;
import com.propertyManagement.business.mapper.WxAccountMapper;
import com.propertyManagement.business.service.IWxAccountService;

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

/**
 * 员工账号Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-22
 */
@RequiredArgsConstructor
@Service
public class WxAccountServiceImpl implements IWxAccountService {

    private final WxAccountMapper baseMapper;
    private final WxCommunityMapper communityMapper;

    /**
     * 查询员工账号
     */
    @Override
    public WxAccountVo queryById(Long accountId){
        return baseMapper.selectVoById(accountId);
    }

    /**
     * 查询员工账号列表
     */
    @Override
    public TableDataInfo<WxAccountVo> queryPageList(WxAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxAccount> lqw = buildQueryWrapper(bo);
        Page<WxAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工账号列表
     */
    @Override
    public List<WxAccountVo> queryList(WxAccountBo bo) {
        LambdaQueryWrapper<WxAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxAccount> buildQueryWrapper(WxAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxAccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxAccount::getCommunityId, bo.getCommunityId());
        lqw.like(StringUtils.isNotBlank(bo.getCommunityName()), WxAccount::getCommunityName, bo.getCommunityName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WxAccount::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAccount()), WxAccount::getAccount, bo.getAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getPossword()), WxAccount::getPossword, bo.getPossword());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), WxAccount::getMobile, bo.getMobile());
        lqw.eq(bo.getSex() != null, WxAccount::getSex, bo.getSex());
        lqw.eq(bo.getType() != null, WxAccount::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增员工账号
     */
    @Override
    public Boolean insertByBo(WxAccountBo bo) {
        WxAccount add = BeanUtil.toBean(bo, WxAccount.class);
        validEntityBeforeSave(add);
        WxCommunity community = communityMapper.selectById(add.getCommunityId());
        add.setCommunityName(community.getCommunityName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAccountId(add.getAccountId());
        }
        return flag;
    }

    /**
     * 修改员工账号
     */
    @Override
    public Boolean updateByBo(WxAccountBo bo) {
        WxAccount update = BeanUtil.toBean(bo, WxAccount.class);
        validEntityBeforeSave(update);
        WxCommunity community = communityMapper.selectById(update.getCommunityId());
        update.setCommunityName(community.getCommunityName());
        return baseMapper.updateById(update) > 0;
    }

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

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