package org.dromara.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.dromara.biz.domain.vo.PoliceCapacityTree;
import org.dromara.common.core.constant.UserConstants;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.TreeBuildUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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.dromara.system.domain.SysDept;
import org.dromara.system.domain.SysOss;
import org.dromara.system.domain.bo.SysDeptBo;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.mapper.SysOssMapper;
import org.springframework.stereotype.Service;
import org.dromara.biz.domain.bo.BizPoliceCapacityBo;
import org.dromara.biz.domain.vo.BizPoliceCapacityVo;
import org.dromara.biz.domain.BizPoliceCapacity;
import org.dromara.biz.mapper.BizPoliceCapacityMapper;
import org.dromara.biz.service.IBizPoliceCapacityService;

import java.util.*;

/**
 * 警种能力Service业务层处理
 *
 * @author Luo.J
 * @date 2025-05-09
 */
@RequiredArgsConstructor
@Service
public class BizPoliceCapacityServiceImpl implements IBizPoliceCapacityService {

    private final BizPoliceCapacityMapper baseMapper;

    private final SysDeptMapper sysDeptMapper;

    private final SysOssMapper sysOssMapper;

    /**
     * 查询警种能力
     *
     * @param id 主键
     * @return 警种能力
     */
    @Override
    public BizPoliceCapacityVo queryById(Long id){
        BizPoliceCapacityVo bizPoliceCapacityVo = baseMapper.selectVoById(id);
        if (ObjectUtil.isNotEmpty(bizPoliceCapacityVo.getOssId())){
            setOssList(bizPoliceCapacityVo);
        }
        return bizPoliceCapacityVo;
    }

    @Override
    public List<PoliceCapacityTree> selectPoliceCapacityTreeList(String name) {
        List<PoliceCapacityTree> list = new ArrayList<>();

        List<Long> deptIdList = baseMapper.selectDeptIdList(name);
        if (CollUtil.isNotEmpty(deptIdList)){
            for (Long deptId : deptIdList) {
                PoliceCapacityTree policeCapacityTree = new PoliceCapacityTree();
                List<BizPoliceCapacity> bizPoliceCapacityList = baseMapper.selectByDept(deptId);
                policeCapacityTree.setName(bizPoliceCapacityList.get(0).getDeptName());
                policeCapacityTree.setDeptId(bizPoliceCapacityList.get(0).getDeptId());
                policeCapacityTree.setId(IdUtil.getSnowflake().nextId());
                policeCapacityTree.setChildren(bizPoliceCapacityList);
                list.add(policeCapacityTree);
            }
        }
        return list;
    }

    /**
     * 分页查询警种能力列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 警种能力分页列表
     */
    @Override
    public TableDataInfo<BizPoliceCapacityVo> queryPageList(BizPoliceCapacityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BizPoliceCapacity> lqw = buildQueryWrapper(bo);
        IPage<BizPoliceCapacityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BizPoliceCapacityVo> records = result.getRecords();
        records.forEach(item -> {
            if (StringUtils.isNotBlank(item.getOssId())){
                setOssList(item);
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     *  根据ossId查询附件信息一并返回
     */
    private void setOssList(BizPoliceCapacityVo bizPoliceCapacityVo) {
        List<SysOss> ossList = new ArrayList<>();
        String[] split = bizPoliceCapacityVo.getOssId().split(",");
        for (String ossId : split) {
            SysOss sysOss = sysOssMapper.selectById(ossId);
            ossList.add(sysOss);
        }
        bizPoliceCapacityVo.setOssList(ossList);
    }

    /**
     * 查询符合条件的警种能力列表
     *
     * @param bo 查询条件
     * @return 警种能力列表
     */
    @Override
    public List<BizPoliceCapacityVo> queryList(BizPoliceCapacityBo bo) {
        LambdaQueryWrapper<BizPoliceCapacity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BizPoliceCapacity> buildQueryWrapper(BizPoliceCapacityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BizPoliceCapacity> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BizPoliceCapacity::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getPoliceType()), BizPoliceCapacity::getPoliceType, bo.getPoliceType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BizPoliceCapacity::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), BizPoliceCapacity::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getTag()), BizPoliceCapacity::getTag, bo.getTag());
        lqw.eq(bo.getDeptId() != null, BizPoliceCapacity::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getNeedApproval()), BizPoliceCapacity::getNeedApproval, bo.getNeedApproval());
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), BizPoliceCapacity::getOssId, bo.getOssId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BizPoliceCapacity::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增警种能力
     *
     * @param bo 警种能力
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BizPoliceCapacityBo bo) {
        if (ObjectUtil.isNotEmpty(bo.getDeptId())){
            bo.setDeptName(sysDeptMapper.selectById(bo.getDeptId()).getDeptName());
        }
        BizPoliceCapacity add = MapstructUtils.convert(bo, BizPoliceCapacity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改警种能力
     *
     * @param bo 警种能力
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BizPoliceCapacityBo bo) {
        if (ObjectUtil.isNotEmpty(bo.getDeptId())){
            bo.setDeptName(sysDeptMapper.selectById(bo.getDeptId()).getDeptName());
        }
        BizPoliceCapacity update = MapstructUtils.convert(bo, BizPoliceCapacity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除警种能力信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
