package com.mosty.base.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.jcgl.TbJcglXlfaDto;
import com.mosty.base.model.dto.jcgl.TbJcglXlfaXldwDto;
import com.mosty.base.model.entity.jcgl.TbJcglXlfa;
import com.mosty.base.model.entity.jcgl.TbJcglXlfaXldw;
import com.mosty.base.model.query.jcgl.TbJcglXlfaQuery;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.manager.mapper.TbJcglXlfaMapper;
import com.mosty.base.manager.mapper.TbJcglXlfaXldwMapper;
import com.mosty.base.manager.service.TbJcglXlfaService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @author dw
 * @since 2022/8/11
 **/
@Service
@AllArgsConstructor
public class TbJcglXlfaServiceImpl extends ServiceImpl<TbJcglXlfaMapper, TbJcglXlfa> implements TbJcglXlfaService {

    private final TbJcglXlfaXldwMapper tbJcglXlfaXldwMapper;

    @Override
    @Transactional
    public int addEntity(TbJcglXlfaDto dto) {
        TbJcglXlfa fa = new TbJcglXlfa();
        BeanUtils.copyProperties(dto, fa);
        fa.setXtSjly("1");
        UserInfo user = UserInfoManager.get();
        fa.setId(UUIDGenerator.getUUID());
        this.saveOther(dto, fa, user);
        return this.baseMapper.insert(fa);
    }

    @Override
    @Transactional
    public int updateEntity(TbJcglXlfaDto dto) {
        TbJcglXlfa fa = new TbJcglXlfa();
        BeanUtils.copyProperties(dto, fa);
        // 删除字表中的数据
        this.deleteByFaid(dto.getId());
        // 重新添加数据
        this.saveOther(dto, fa, null);
        return this.baseMapper.updateById(fa);
    }

    @Override
    @Transactional
    public int delEntity(List<String> ids) {
        if (ids.size() > 0) {
            ids.forEach(this.tbJcglXlfaXldwMapper::deleteEntity);
        }
        return this.baseMapper.deleteBatchIds(ids);
    }

    @Override
    public IPage<TbJcglXlfaDto> getPageList(TbJcglXlfaQuery dto) {
        IPage<TbJcglXlfa> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        page = this.baseMapper.selectPage(page,
                new LambdaQueryWrapper<TbJcglXlfa>()
                        .like(!StringUtils.isEmpty(dto.getFamc()), TbJcglXlfa::getFamc, dto.getFamc())
                        .eq(!StringUtils.isEmpty(dto.getZxzq()), TbJcglXlfa::getZxzq, dto.getZxzq())
                        .like(!StringUtils.isEmpty(dto.getZxms()), TbJcglXlfa::getZxms, dto.getZxms())
                        .eq(dto.getZxksyr() != null, TbJcglXlfa::getZxksyr, dto.getZxksyr())
                        .eq(dto.getZxxq() != null, TbJcglXlfa::getZxxq, dto.getZxxq())
                        .eq(!StringUtils.isEmpty(dto.getSsbmdm()), TbJcglXlfa::getSsbmdm, dto.getSsbmdm())
                        .eq(!StringUtils.isEmpty(dto.getSsbmid()), TbJcglXlfa::getSsbmid, dto.getSsbmid()));
        List<TbJcglXlfa> faList = page.getRecords();
        List<TbJcglXlfaDto> dtoList = new ArrayList<>();
        faList.forEach(item -> {
            TbJcglXlfaDto faDto = new TbJcglXlfaDto();
            BeanUtils.copyProperties(item, faDto);
            // 查询巡逻点位
            List<TbJcglXlfaXldw> dwList = this.tbJcglXlfaXldwMapper.getListByFaId(item.getId());
            List<TbJcglXlfaXldwDto> dwDtoList = new ArrayList<>();
            dwList.forEach(dw -> {
                TbJcglXlfaXldwDto xldwDto = new TbJcglXlfaXldwDto();
                BeanUtils.copyProperties(dw, xldwDto);
                if (dw.getQy() != null) {
                    xldwDto.setFw(JtsUtils.decodePolygon(dw.getQy()));
                }
                dwDtoList.add(xldwDto);
            });
            faDto.setXldwList(dwDtoList);
            dtoList.add(faDto);
        });
        IPage<TbJcglXlfaDto> newPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        newPage.setTotal(page.getTotal());
        newPage.setRecords(dtoList);
        return newPage;
    }

    @Override
    public TbJcglXlfaDto getInfo(String id) {
        TbJcglXlfa fa = this.baseMapper.selectById(id);
        if (fa != null) {
            TbJcglXlfaDto faDto = new TbJcglXlfaDto();
            BeanUtils.copyProperties(fa, faDto);
            // 查询巡逻点位
            List<TbJcglXlfaXldw> dwList = this.tbJcglXlfaXldwMapper.getListByFaId(fa.getId());
            List<TbJcglXlfaXldwDto> dwDtoList = new ArrayList<>();
            dwList.forEach(dw -> {
                TbJcglXlfaXldwDto xldwDto = new TbJcglXlfaXldwDto();
                BeanUtils.copyProperties(dw, xldwDto);
                if (dw.getQy() != null) {
                    xldwDto.setFw(JtsUtils.decodePolygon(dw.getQy()));
                }
                dwDtoList.add(xldwDto);
            });
            faDto.setXldwList(dwDtoList);
            return faDto;
        }
        return null;
    }

    // 删除
    private void deleteByFaid(String id) {
        this.tbJcglXlfaXldwMapper.deleteEntity(id);
    }

    // 保存巡逻方案的其他信息
    private void saveOther(TbJcglXlfaDto dto, TbJcglXlfa fa, UserInfo user) {
        if (user != null) {
            fa.setFazdrId(String.valueOf(user.getUserId()));
            fa.setFazdrXm(user.getUserName());
            fa.setFazdrSfzh(user.getIdEntityCard());
            fa.setFazdsj(new Date());
            fa.setSsbm(user.getDeptName());
            fa.setSsbmid(String.valueOf(user.getDeptId()));
            fa.setSsbmdm(user.getDeptCode());
            fa.setSsxgaj(user.getFxjDeptName());
            fa.setSsxgajid(String.valueOf(user.getFxjDeptId()));
            fa.setSsxgajdm(user.getFxjDeptCode());
            fa.setSssgaj(user.getDszDeptName());
            fa.setSssgajid(String.valueOf(user.getDszDeptId()));
            fa.setSssgajdm(user.getDszDeptCode());
        }
        List<TbJcglXlfaXldwDto> xldwList = dto.getXldwList();
        if (!CollectionUtils.isEmpty(xldwList)) {
            xldwList.forEach(xldw -> {
                TbJcglXlfaXldw item = new TbJcglXlfaXldw();
                BeanUtils.copyProperties(xldw, item);
                item.setFaId(fa.getId());
                item.setXtSjly("1");
                if (xldw.getJd() != null && xldw.getWd() != null) {
                    item.setZbhash(GeoHashKit.encode(xldw.getJd(), xldw.getWd()));
                    item.setZb(JtsUtils.getPoint(xldw.getJd(), xldw.getWd()));
                }
                if (!CollectionUtils.isEmpty(xldw.getFw())) {
                    item.setQy(JtsUtils.getPolygon(xldw.getFw()));
                }
                item.setFaId(fa.getId());
                this.tbJcglXlfaXldwMapper.insertEntity(item);
            });
        }
    }
}
