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.LiveBillCreateType;
import com.propertyManagement.common.enums.LiveBillType;
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.WxLiveBillBo;
import com.propertyManagement.business.domain.vo.WxLiveBillVo;
import com.propertyManagement.business.domain.WxLiveBill;
import com.propertyManagement.business.mapper.WxLiveBillMapper;
import com.propertyManagement.business.service.IWxLiveBillService;

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

/**
 * 生活缴费账单Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxLiveBillServiceImpl implements IWxLiveBillService {

    private final WxLiveBillMapper baseMapper;

    /**
     * 查询生活缴费账单
     */
    @Override
    public WxLiveBillVo queryById(Long billId){
        return baseMapper.selectVoById(billId);
    }

    /**
     * 查询生活缴费账单列表
     */
    @Override
    public TableDataInfo<WxLiveBillVo> queryPageList(WxLiveBillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxLiveBill> lqw = buildQueryWrapper(bo);
        Page<WxLiveBillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生活缴费账单列表
     */
    @Override
    public List<WxLiveBillVo> queryList(WxLiveBillBo bo) {
        LambdaQueryWrapper<WxLiveBill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }


    private LambdaQueryWrapper<WxLiveBill> buildQueryWrapper(WxLiveBillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxLiveBill> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, WxLiveBill::getCommunityId, bo.getCommunityId());
        lqw.like(bo.getCommunityName() != null, WxLiveBill::getCommunityName, bo.getCommunityName());
        lqw.like(bo.getProprietorId() != null, WxLiveBill::getProprietorId, bo.getProprietorId());
        lqw.like(StringUtils.isNotBlank(bo.getProprietorName()), WxLiveBill::getProprietorName, bo.getProprietorName());
        lqw.like(StringUtils.isNotBlank(bo.getProprietorPhone()), WxLiveBill::getProprietorPhone, bo.getProprietorPhone());
        lqw.like(StringUtils.isNotBlank(bo.getRoom()), WxLiveBill::getRoom, bo.getRoom());
        lqw.like(StringUtils.isNotBlank(bo.getPlateNumber()), WxLiveBill::getPlateNumber, bo.getPlateNumber());
//        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), WxLiveBill::getMonth, bo.getMonth());
        lqw.eq(bo.getType() != null, WxLiveBill::getType, bo.getType());
        lqw.eq(bo.getState() != null, WxLiveBill::getState, bo.getState());
        lqw.eq(bo.getCarportId() != null, WxLiveBill::getCarportId, bo.getCarportId());
        lqw.eq(bo.getCreateType() != null, WxLiveBill::getCreateType, bo.getCreateType());
        lqw.orderByDesc(WxLiveBill::getCreateTime);
        return lqw;
    }

    @Override
    public WxLiveBillBo add(WxLiveBillBo bo) {
        WxLiveBill add = BeanUtil.toBean(bo, WxLiveBill.class);
        LoginWxUser wxUser = AuthUtil.getWxUser();
        add.setCommunityId(wxUser.getCommunityId());
        add.setCommunityName(wxUser.getCommunityName());
        add.setProprietorId(wxUser.getProprietorId());
        add.setProprietorName(wxUser.getName());
        add.setProprietorPhone(wxUser.getMobile());
        add.setRoom(wxUser.getRoom());
        add.setSqm(bo.getSqm());
        validEntityBeforeSave(add);
        add.setCreateType(LiveBillCreateType.ONE.getCode());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBillId(add.getBillId());
        }
        return bo;
    }

    /**
     * 新增生活缴费账单
     */
    @Override
    public Boolean insertByBo(WxLiveBillBo bo) {
        WxLiveBill add = BeanUtil.toBean(bo, WxLiveBill.class);
        add.setCreateType(LiveBillCreateType.TWO.getCode());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBillId(add.getBillId());
        }
        return flag;
    }

    /**
     * 修改生活缴费账单
     */
    @Override
    public Boolean updateByBo(WxLiveBillBo bo) {
        WxLiveBill update = BeanUtil.toBean(bo, WxLiveBill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WxLiveBill entity){
        switch (entity.getType()){
            case 1:
            case 2:
                if (entity.getMoney() == null) {
                    throw new RuntimeException("缴纳金额不能为空");
                }
                break;
            case 3:
                if (entity.getMoney() == null) {
                    throw new RuntimeException("缴纳金额不能为空");
                }
                if (null == entity.getDuration()) {
                    throw new RuntimeException("缴费时长不能为空");
                }
                break;
            case 4:
                if (entity.getMoney() == null) {
                    throw new RuntimeException("缴纳金额不能为空");
                }
                if (null == entity.getCarportId()) {
                    throw new RuntimeException("车位不能为空");
                }
                break;
            case 5:
                if (entity.getMoney() == null) {
                    throw new RuntimeException("缴纳金额不能为空");
                }
                if (null == entity.getCarportId()) {
                    throw new RuntimeException("车位不能为空");
                }
                if (null == entity.getDuration()) {
                    throw new RuntimeException("缴费时长不能为空");
                }
                break;
            case 6:
                if (null == entity.getStoreroomId()) {
                    throw new RuntimeException("储藏室不能为空");
                }
                break;
            default:
                if (null == entity.getProjectName()) {
                    throw new RuntimeException("项目名称不能为空");
                }
                if (null == entity.getProjectInfo()) {
                    throw new RuntimeException("项目描述不能为空");
                }
                if (entity.getMoney() == null) {
                    throw new RuntimeException("缴纳金额不能为空");
                }
                break;
        }
        //TODO 做一些数据校验,如唯一约束
    }

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