package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.business.domain.WxAccount;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.mapper.WxAccountMapper;
import com.propertyManagement.business.support.util.AuthUtil;
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.WxClockBo;
import com.propertyManagement.business.domain.vo.WxClockVo;
import com.propertyManagement.business.domain.WxClock;
import com.propertyManagement.business.mapper.WxClockMapper;
import com.propertyManagement.business.service.IWxClockService;

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

/**
 * 打卡Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxClockServiceImpl implements IWxClockService {

    private final WxClockMapper baseMapper;
    private final WxAccountMapper accountMapper;

    /**
     * 查询打卡
     */
    @Override
    public WxClockVo queryById(Long clockId){
        return baseMapper.selectVoById(clockId);
    }

    /**
     * 查询打卡列表
     */
    @Override
    public TableDataInfo<WxClockVo> queryPageList(WxClockBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxClock> lqw = buildQueryWrapper(bo);
        Page<WxClockVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询打卡列表
     */
    @Override
    public List<WxClockVo> queryList(WxClockBo bo) {
        LambdaQueryWrapper<WxClock> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public Boolean add(WxClockBo bo) {
        WxClock add = BeanUtil.toBean(bo, WxClock.class);
        validEntityBeforeSave(add);
        LoginWxUser wxUser = AuthUtil.getWxUser();
        add.setCommunityId(wxUser.getCommunityId());
        add.setCommunityName(wxUser.getCommunityName());
        add.setAccountId(wxUser.getAccountId());
        add.setAccountName(wxUser.getAccountName());
        add.setClockTime(new Date());
        WxAccount wxAccount = accountMapper.selectById(wxUser.getAccountId());
        add.setClockUserRole(wxAccount.getType());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClockId(add.getClockId());
        }
        return flag;
    }

    private LambdaQueryWrapper<WxClock> buildQueryWrapper(WxClockBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxClock> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClockTime() != null, WxClock::getClockTime, bo.getClockTime());
        lqw.eq(bo.getAccountId() != null, WxClock::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), WxClock::getAccountName, bo.getAccountName());
        lqw.eq(bo.getCommunityId() != null, WxClock::getCommunityId, bo.getCommunityId());
        lqw.like(bo.getCommunityName() != null, WxClock::getCommunityName, bo.getCommunityName());
        lqw.eq(bo.getClockTime() != null, WxClock::getClockTime, bo.getClockTime());
        lqw.eq(bo.getClockUserRole() != null, WxClock::getClockUserRole, bo.getClockUserRole());
        lqw.between(bo.getClockStartTime() != null, WxClock::getClockTime, bo.getClockStartTime(), bo.getClockEndTime());
        return lqw;
    }

    /**
     * 新增打卡
     */
    @Override
    public Boolean insertByBo(WxClockBo bo) {
        WxClock add = BeanUtil.toBean(bo, WxClock.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClockId(add.getClockId());
        }
        return flag;
    }

    /**
     * 修改打卡
     */
    @Override
    public Boolean updateByBo(WxClockBo bo) {
        WxClock update = BeanUtil.toBean(bo, WxClock.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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