package com.mosty.yszx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.yszx.TbYsRyDto;
import com.mosty.base.model.entity.yszx.TbYsRy;
import com.mosty.base.model.entity.yszx.TbYsRyBq;
import com.mosty.base.model.entity.yszx.TbYsRyDh;
import com.mosty.base.model.vo.yszx.*;
import com.mosty.yszx.mapper.TbYsRyBqMapper;
import com.mosty.yszx.mapper.TbYsRyDhMapper;
import com.mosty.yszx.mapper.TbYsRyMapper;
import com.mosty.yszx.service.TbYsRyService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class TbYsRyServiceImpl extends ServiceImpl<TbYsRyMapper, TbYsRy>
        implements TbYsRyService {

    private final TbYsRyDhMapper tbYsRyDhMapper;
    private final TbYsRyBqMapper tbYsRyBqMapper;

    @Override
    public TbYsRyVo getRyInfo(String sfzh) {
        TbYsRy ry = this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbYsRy>()
                        .eq(TbYsRy::getZjhm, sfzh)
                        .eq(TbYsRy::getXtSjzt, "1")
        );
        if (ry != null) {
            TbYsRyVo vo = new TbYsRyVo();
            BeanUtils.copyProperties(ry, vo);
            // 查询人员电话
            List<TbYsRyDh> dhList = this.tbYsRyDhMapper.selectList(
                    new LambdaQueryWrapper<TbYsRyDh>()
                            .eq(TbYsRyDh::getRyid, ry.getId())
                            .eq(TbYsRyDh::getXtSjzt, "1")
            );
            List<TbYsRyDhVo> dhVos = new ArrayList<>();
            dhList.forEach(dh -> {
                TbYsRyDhVo dhVo = new TbYsRyDhVo();
                BeanUtils.copyProperties(dh, dhVo);
                dhVos.add(dhVo);
            });
            vo.setDhList(dhVos);
            // 查询人员标签
            List<TbYsRyBq> bqList = this.tbYsRyBqMapper.selectList(
                    new LambdaQueryWrapper<TbYsRyBq>()
                            .eq(TbYsRyBq::getRyid, ry.getId())
                            .eq(TbYsRyBq::getXtSjzt, "1")
            );
            List<TbYsRyBqVo> bqVos = new ArrayList<>();
            bqList.forEach(bq -> {
                TbYsRyBqVo bqVo = new TbYsRyBqVo();
                BeanUtils.copyProperties(bq, bqVo);
                bqVos.add(bqVo);
            });
            vo.setBqList(bqVos);
            return vo;
        }
        return null;
    }

    @Override
    public void addRyYs(TbYsRyDto dto) {
        TbYsRy ry = new TbYsRy();
        BeanUtils.copyProperties(dto, ry);
        this.baseMapper.insert(ry);
    }

    @Override
    public void updateRyYs(TbYsRyDto dto) {
        TbYsRy ry = new TbYsRy();
        BeanUtils.copyProperties(dto, ry);
        this.baseMapper.updateById(ry);
    }

    @Override
    public void updateRyBqYs(TbYsRyBqUpdateDto dto) {
        TbYsRy ry = this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbYsRy>()
                        .eq(TbYsRy::getZjhm, dto.getSfzh())
                        .eq(TbYsRy::getXtSjzt, "1")
        );
        if (ry != null) {
            // 删除人员标签（新加的数据）
            this.tbYsRyBqMapper.deleteByRyId(ry.getId());
            // 新增人员标签
            TbYsRyBq bq = new TbYsRyBq();
            bq.setRyid(ry.getId());
            bq.setZjzl("1");
            bq.setZjhm(ry.getZjhm());
            bq.setBqdm(dto.getBqdm());
            bq.setBqmc(dto.getBqmc());
            bq.setBqrq(new Date());
            bq.setXtSjly("1");
            this.tbYsRyBqMapper.insert(bq);
        }
    }

    @Override
    public void updateRyDhys(TbYsRyDhUpdateDto dto) {
        TbYsRy ry = this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbYsRy>()
                        .eq(TbYsRy::getZjhm, dto.getSfzh())
                        .eq(TbYsRy::getXtSjzt, "1")
        );
        if (ry != null) {
            // 删除人员电话（新加的数据）
            this.tbYsRyDhMapper.deleteByRyId(ry.getId());
            for (int i = 0; i < dto.getList().size(); i++) {
                Map<String, String> map = dto.getList().get(i);
                // 新增人员电话
                TbYsRyDh dh = new TbYsRyDh();
                dh.setRyid(ry.getId());
                dh.setZjzl("1");
                dh.setXm(ry.getXm());
                dh.setZjhm(ry.getZjhm());
                dh.setLxdh(map.get("lxdh"));
                dh.setImsi(map.get("imsi"));
                dh.setXtSjly("1");
                this.tbYsRyDhMapper.insert(dh);
            }
        }
    }
}
