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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.model.dto.yszx.TbYsGajgDto;
import com.mosty.base.model.entity.jcgl.TbJcglJwz;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.manager.mapper.TbJcglJwzMapper;
import com.mosty.base.manager.remote.TbYsZxAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglJwzService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@AllArgsConstructor
@Service
public class TbJcglJwzServiceImpl extends ServiceImpl<TbJcglJwzMapper, TbJcglJwz>
        implements TbJcglJwzService {

    private final TbYsZxAdaptRemoteService tbYsZxAdaptRemoteService;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbJcglJwz> selectJwzList(TbJcglJwz jwz, IPage<TbJcglJwz> page) {
        QueryWrapper<TbJcglJwz> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        jwz.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(jwz.getSsbmdm(), null));
        qw.lambda().likeRight(StringUtils.isNotBlank(jwz.getSsbmdm()), TbJcglJwz::getSsbmdm, jwz.getSsbmdm())
                .eq(StringUtils.isNotBlank(jwz.getJwzLx()), TbJcglJwz::getJwzLx, jwz.getJwzLx())
                .like(StringUtils.isNotBlank(jwz.getJwzMc()), TbJcglJwz::getJwzMc, jwz.getJwzMc())
                .like(StringUtils.isNotBlank(jwz.getFzrXm()), TbJcglJwz::getFzrXm, jwz.getFzrXm())
                .eq(TbJcglJwz::getXtScbz, 0);
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    @Transactional
    public void addJwz(TbJcglJwz jwz) {
        jwz.setJwzId(UUIDGenerator.getUUID());
        if (StringUtils.isNotBlank(jwz.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(jwz.getSsbmdm());
            jwz.setSsbm(dept.getDeptname());
            jwz.setSsbmid(dept.getDeptid());
            jwz.setSsbmdm(dept.getDeptcode());
            jwz.setSsxgajdm(dept.getFxjcode());
            jwz.setSsxgajid(dept.getFxjid());
            jwz.setSsxgaj(dept.getFxjname());
            jwz.setSssgaj(dept.getDszname());
            jwz.setSssgajdm(dept.getDszcode());
            jwz.setSssgajid(dept.getDszid());
        }
        if (jwz.getJd() != null && jwz.getWd() != null) {
            jwz.setZb(JtsUtils.getPoint(jwz.getJd(), jwz.getWd()));
            jwz.setZbhash(GeoHashKit.encode(jwz.getJd(), jwz.getWd()));
        }
        jwz.setXtSjly("1");
        this.baseMapper.insertEntity(jwz);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(jwz.getJwzId());
        ysGajgDto.setLybm("tb_jcgl_jwz");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001007");
        ysGajgDto.setGajgmc(jwz.getJwzMc());
        ysGajgDto.setGajgjc(jwz.getJwzMc());
        ysGajgDto.setWzXzqh(jwz.getXzqhdm());
        ysGajgDto.setWzXz(jwz.getJwzDz());
        ysGajgDto.setWzBc(jwz.getJwzDz());
        ysGajgDto.setJd(jwz.getJd());
        ysGajgDto.setWd(jwz.getWd());
        ysGajgDto.setSsbm(jwz.getSsbm());
        ysGajgDto.setSsbmid(jwz.getSsbmid());
        ysGajgDto.setSsbmdm(jwz.getSsbmdm());
        tbYsZxAdaptRemoteService.addGajg(ysGajgDto);
    }

    @Override
    @Transactional
    public void updateJwz(TbJcglJwz jwz) {
        if (jwz.getJd() != null && jwz.getWd() != null) {
            jwz.setZb(JtsUtils.getPoint(jwz.getJd(), jwz.getWd()));
            jwz.setZbhash(GeoHashKit.encode(jwz.getJd(), jwz.getWd()));
        }
        if (StringUtils.isNotBlank(jwz.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(jwz.getSsbmdm());
            jwz.setSsbm(dept.getDeptname());
            jwz.setSsbmid(dept.getDeptid());
            jwz.setSsbmdm(dept.getDeptcode());
            jwz.setSsxgajdm(dept.getFxjcode());
            jwz.setSsxgajid(dept.getFxjid());
            jwz.setSsxgaj(dept.getFxjname());
            jwz.setSssgaj(dept.getDszname());
            jwz.setSssgajdm(dept.getDszcode());
            jwz.setSssgajid(dept.getDszid());
        }
        this.baseMapper.updateEntity(jwz);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(jwz.getJwzId());
        ysGajgDto.setLybm("tb_jcgl_jwz");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001007");
        ysGajgDto.setGajgmc(jwz.getJwzMc());
        ysGajgDto.setGajgjc(jwz.getJwzMc());
        ysGajgDto.setWzXzqh(jwz.getXzqhdm());
        ysGajgDto.setWzXz(jwz.getJwzDz());
        ysGajgDto.setWzBc(jwz.getJwzDz());
        ysGajgDto.setJd(jwz.getJd());
        ysGajgDto.setWd(jwz.getWd());
        ysGajgDto.setSsbm(jwz.getSsbm());
        ysGajgDto.setSsbmid(jwz.getSsbmid());
        ysGajgDto.setSsbmdm(jwz.getSsbmdm());
        tbYsZxAdaptRemoteService.updateGajg(ysGajgDto);
    }

    @Override
    @Transactional
    public boolean deleteJwz(List<String> id) {
        if (id.size() > 0) {
            this.baseMapper.deleteJwz(id);
        }
        for (String s : id) {
            TbYsGajgDto ysGajgDto = new TbYsGajgDto();
            ysGajgDto.setLyid(s);
            ysGajgDto.setLybm("tb_jcgl_jwz");
            tbYsZxAdaptRemoteService.delEntity(ysGajgDto);
        }
        return true;
    }

    @Override
    public Object selectByid(String id) {
        return this.baseMapper.selectOne(
                new QueryWrapper<TbJcglJwz>()
                        .eq("jwz_id", id)
                        .eq("xt_scbz", 0)
        );
    }
}
