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.TbZdxlFgdwUpdateDTO;
import com.mosty.base.model.entity.yjzl.zddw.TbZdxlFgdw;
import com.mosty.base.model.query.yjzl.TbZdxlFgdwQuery;
import com.mosty.base.model.vo.yjzl.TbZdxlFgdwVO;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.QueryWrapperUtils;
import com.mosty.yjzl.mapper.TbZdxlFgdwMapper;
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/05/15 21:55
 * @since 2025/05/15 21:55
 */
@Service
@AllArgsConstructor
public class TbZdxlFgdwServiceImpl extends ServiceImpl<TbZdxlFgdwMapper, TbZdxlFgdw> implements TbZdxlFgdwService {

    @Override
    public TbZdxlFgdwVO selectById(Long id) {
        if(ObjectUtils.isEmpty(id)){
            return null;
        }
        TbZdxlFgdw entity = this.baseMapper.selectOne(new QueryWrapper<TbZdxlFgdw>()
                .eq(TbZdxlFgdw.ID_FIELD, id)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
        return this.buildAllInfoByEntity(entity);
    }

    @Override
    public TbZdxlFgdwVO selectByBddId(String bddId) {
        if(StringUtils.isBlank(bddId)){
            return null;
        }
        TbZdxlFgdw entity = this.baseMapper.selectOne(new QueryWrapper<TbZdxlFgdw>()
                .inSql(TbZdxlFgdw.ID_FIELD, "select fgdw_id from tb_zdxl_fgdw_bdd where xt_sjzt = '1' and xt_scbz = '0' and id = '" + bddId + "' ")
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
        return this.buildAllInfoByEntity(entity);
    }

    @Override
    public List<TbZdxlFgdwVO> selectAllList() {
        return this.buildAllInfoListByEntityList(this.baseMapper.selectList(new QueryWrapper<>()));
    }

    @Override
    public IPage<TbZdxlFgdwVO> selectPage(TbZdxlFgdwQuery query) {
        IPage<TbZdxlFgdw> page = PageUtils.buildPage(query.getPageSize(), query.getPageCurrent());
        QueryWrapper<TbZdxlFgdw> qw = new QueryWrapper<>();

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

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

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

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

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

    @Override
    public List<TbZdxlFgdwVO> selectList(TbZdxlFgdwQuery query) {
        QueryWrapper<TbZdxlFgdw> qw = new QueryWrapper<>();

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

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long updateEntity(TbZdxlFgdwUpdateDTO dto) {
        //复制参数至实体
        TbZdxlFgdw entity = BeanUtil.copyProperties(dto, TbZdxlFgdw.class);
        entity.setMc1(dto.getMc());

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

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

        //方格点位ID
        qw.like(ObjectUtils.isNotEmpty(query.getMc()), TbZdxlFgdw.MC, query.getMc());
        
    }
    
    /**
     * 组装全量信息列表（根据实体列表）
     * @param entityList 实体列表
     * @return 全量信息列表
     */
    private List<TbZdxlFgdwVO> buildAllInfoListByEntityList(List<TbZdxlFgdw> entityList){
        if(CollectionUtils.isEmpty(entityList)){
            return null;
        }
        List<TbZdxlFgdwVO> allInfoVOList = new ArrayList<>();
        //组装返回数据
        for(TbZdxlFgdw entity : entityList){
            TbZdxlFgdwVO resultVO = this.buildAllInfoByEntity(entity);
            allInfoVOList.add(resultVO);
        }
        //返回列表
        if(CollectionUtils.isEmpty(allInfoVOList)){
            return null;
        }
        return allInfoVOList;
    }

    /**
     * 组装全量信息（根据实体信息）
     * @param entity 实体信息
     * @return 全量信息
     */
    private TbZdxlFgdwVO buildAllInfoByEntity(TbZdxlFgdw entity){
        if(ObjectUtils.isEmpty(entity)){
            return null;
        }
        TbZdxlFgdwVO resultVO = BeanUtil.copyProperties(entity, TbZdxlFgdwVO.class);

        //组装方格其他数据
        resultVO.setMc(resultVO.getMc1());
        resultVO.setX1(resultVO.getX11());
        resultVO.setY1(resultVO.getY11());
        resultVO.setX2(resultVO.getX21());
        resultVO.setY2(resultVO.getY21());

        return resultVO;
    }

}
