package com.mosty.yjzl.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.yjzl.TbZdxlFgdwBddSaveDTO;
import com.mosty.base.model.dto.yjzl.TbZdxlFgdwBddUpdateDTO;
import com.mosty.base.model.entity.yjzl.zddw.TbZdxlFgdw;
import com.mosty.base.model.entity.yjzl.zddw.TbZdxlFgdwBdd;
import com.mosty.base.model.query.yjzl.TbZdxlFgdwBddQuery;
import com.mosty.base.model.vo.yjzl.TbZdxlFgdwBddVO;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.QueryWrapperUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.yjzl.mapper.TbZdxlFgdwBddMapper;
import com.mosty.yjzl.service.TbZdxlFgdwBddService;
import com.mosty.yjzl.service.TbZdxlFgdwService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangzhao
 * @description 指导巡逻方格点位必到点表Service实现类
 * @modifier zhangzhao
 * @modifiedTime 2025/09/06 14:16
 * @since 2025/09/06 14:16
 */
@Service
@AllArgsConstructor
public class TbZdxlFgdwBddServiceImpl extends ServiceImpl<TbZdxlFgdwBddMapper, TbZdxlFgdwBdd> implements TbZdxlFgdwBddService {

    /**
     * 指导巡逻方格点位表Service
     */
    private final TbZdxlFgdwService tbZdxlFgdwService;

    @Override
    public TbZdxlFgdwBdd selectById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return this.baseMapper.selectOne(new QueryWrapper<TbZdxlFgdwBdd>()
                .eq(TbZdxlFgdwBdd.ID_FIELD, id)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    public TbZdxlFgdwBdd selectByBddxlrwId(String bddxlrwId) {
        if (StringUtils.isBlank(bddxlrwId)) {
            return null;
        }
        return this.baseMapper.selectOne(new QueryWrapper<TbZdxlFgdwBdd>()
                .inSql(TbZdxlFgdwBdd.ID_FIELD, "select bdd_id from tb_zdxl_fgdw_bddxlrw where xt_sjzt = '1' and xt_scbz = '0' and id = '" + bddxlrwId + "' ")
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    public IPage<TbZdxlFgdwBddVO> selectPage(TbZdxlFgdwBddQuery query) {
        IPage<TbZdxlFgdwBdd> page = PageUtils.buildPage(query.getPageSize(), query.getPageCurrent());
        QueryWrapper<TbZdxlFgdwBdd> qw = new QueryWrapper<>();

        //组装查询参数
        this.buildListSelectQueryWrapper(qw, query);

        //返回Page
        IPage<TbZdxlFgdwBddVO> resultPage = new Page<>(query.getPageCurrent(), query.getPageSize());

        //查询数据
        page = this.baseMapper.selectPage(page, qw);
        List<TbZdxlFgdwBdd> list = page.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return resultPage;
        }

        //转为VO
        List<TbZdxlFgdwBddVO> resultList = this.buildAllInfoListByEntityList(list);

        //组装VO
        resultPage.setRecords(resultList);
        resultPage.setTotal(page.getTotal());
        return resultPage;
    }

    @Override
    public List<TbZdxlFgdwBddVO> selectList(TbZdxlFgdwBddQuery query) {
        QueryWrapper<TbZdxlFgdwBdd> qw = new QueryWrapper<>();

        //组装查询参数
        this.buildListSelectQueryWrapper(qw, query);

        //查询数据
        List<TbZdxlFgdwBdd> list = this.baseMapper.selectList(qw);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }

        //转为VO返回
        return this.buildAllInfoListByEntityList(list);
    }

    @Override
    public List<TbZdxlFgdwBdd> selectListByIdList(List<String> idList) {
        if(CollectionUtils.isEmpty(idList)){
            return null;
        }

        //查询数据
        return this.baseMapper.selectList(new QueryWrapper<TbZdxlFgdwBdd>()
                .in(TbZdxlFgdwBdd.ID_FIELD, idList)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    public List<TbZdxlFgdwBdd> selectListByFgdwId(Long fgdwId) {
        if(ObjectUtils.isEmpty(fgdwId)){
            return null;
        }

        //查询数据
        return this.baseMapper.selectList(new QueryWrapper<TbZdxlFgdwBdd>()
                .eq(TbZdxlFgdwBdd.FGDW_ID_FIELD, fgdwId)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    public List<TbZdxlFgdwBdd> selectListByFgxlrwId(String fgxlrwId) {
        if(ObjectUtils.isEmpty(fgxlrwId)){
            return null;
        }
        List<TbZdxlFgdwBdd> list = this.baseMapper.selectList(new QueryWrapper<TbZdxlFgdwBdd>()
                        .inSql(TbZdxlFgdwBdd.FGDW_ID_FIELD, "select fg_id from tb_zdxl_fgxlrw where xt_sjzt = '1' and xt_scbz = '0' and id = '" + fgxlrwId + "'")
                        .eq("xt_sjzt", "1")
                        .eq("xt_scbz", "0")
        );
        //查询数据
        return list;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveEntity(TbZdxlFgdwBddSaveDTO dto) {
        //复制参数至实体
        TbZdxlFgdwBdd entity = BeanUtil.copyProperties(dto, TbZdxlFgdwBdd.class);

        //组装参数
        entity.setId(UUIDGenerator.getUUID());
        entity.setXtSjly("1");

        //保存实体
        int resultInt = this.baseMapper.insert(entity);
        if(resultInt > 0){
            //返回主键
            return entity.getId();
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateEntity(TbZdxlFgdwBddUpdateDTO dto) {
        //复制参数至实体
        TbZdxlFgdwBdd entity = BeanUtil.copyProperties(dto, TbZdxlFgdwBdd.class);

        //修改实体
        int resultInt = this.baseMapper.updateById(entity);
        if(resultInt > 0){
            //返回主键
            return entity.getId();
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelEntity(TbZdxlFgdwBdd entity) {
        //先修改系统字段
        TbZdxlFgdwBdd cancelEntity = new TbZdxlFgdwBdd();
        cancelEntity.setId(entity.getId());
        this.baseMapper.updateById(cancelEntity);
        //再注销
        return this.baseMapper.deleteById(entity.getId());
    }

    /**
     * 组装查询参数
     * @param qw QueryWrapper
     * @param query 查询对象
     */
    private void buildListSelectQueryWrapper(QueryWrapper<TbZdxlFgdwBdd> qw, TbZdxlFgdwBddQuery query){
        //组装其他排序方式
        QueryWrapperUtils.buildQueryWrapperOrder(qw, query.getSort(), query.getOrder());
        //未注销数据
        qw.eq("xt_sjzt", "1").eq("xt_scbz", "0");

        //方格点位ID
        qw.like(ObjectUtils.isNotEmpty(query.getFgdwId()), TbZdxlFgdwBdd.FGDW_ID_FIELD, query.getFgdwId());
        //方格点位名称
        if(StringUtils.isNotBlank(query.getFgdwMc())){
            qw.inSql(TbZdxlFgdwBdd.FGDW_ID_FIELD, "select id from tb_zdxl_fgdw where xt_sjzt = '1' and xt_scbz = '0' and mc1 = '%" + query.getFgdwMc() + "%'");
        }
        //必到点名称
        qw.like(StringUtils.isNotBlank(query.getBddMc()), TbZdxlFgdwBdd.BDD_MC_FIELD, query.getBddMc());
        //必到点地址
        qw.like(StringUtils.isNotBlank(query.getBddDz()), TbZdxlFgdwBdd.BDD_DZ_FIELD, query.getBddDz());
    }

    /**
     * 组装全量信息列表（根据实体列表）
     * @param entityList 实体列表
     * @return 全量信息列表
     */
    private List<TbZdxlFgdwBddVO> buildAllInfoListByEntityList(List<TbZdxlFgdwBdd> entityList){
        List<TbZdxlFgdwBddVO> allInfoVOList = new ArrayList<>();
        //组装返回数据
        for(TbZdxlFgdwBdd entity : entityList){
            TbZdxlFgdwBddVO resultVO = this.buildAllInfoByEntity(entity);
            allInfoVOList.add(resultVO);
        }
        //返回列表
        if(CollectionUtils.isEmpty(allInfoVOList)){
            return null;
        }
        return allInfoVOList;
    }

    /**
     * 组装全量信息（根据实体信息）
     * @param entity 实体信息
     * @return 全量信息
     */
    private TbZdxlFgdwBddVO buildAllInfoByEntity(TbZdxlFgdwBdd entity){
        TbZdxlFgdwBddVO resultVO = BeanUtil.copyProperties(entity, TbZdxlFgdwBddVO.class);

        if(ObjectUtils.isNotEmpty(resultVO.getFgdwId())){
            //组装方格数据
            TbZdxlFgdw fg = tbZdxlFgdwService.selectById(resultVO.getFgdwId());
            if(ObjectUtils.isNotEmpty(fg)){
                resultVO.setFgdwMc(fg.getMc1());
            }

        }

        return resultVO;
    }

}
