package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.tenant.entity.dto.HandleProvePageDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.ProveDeviceDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.ProveDevicePageDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.ProveDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.po.HandleProve;
import cn.hanghui.outapi.platform.tenant.entity.po.ProveDevice;
import cn.hanghui.outapi.platform.tenant.mapper.HandleProveMapper;
import cn.hanghui.outapi.platform.tenant.mapper.ProveDeviceMapper;
import cn.hanghui.outapi.platform.tenant.service.HandleProveService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.service.impl
 * @class HandleProveImpl
 * @date 2023/12/5 上午11:16
 * @description
 */
@Service
@Slf4j
@Deprecated
public class HandleProveServiceImpl implements HandleProveService {

    @Resource
    private HandleProveMapper handleProveMapper;

    @Resource
    private ProveDeviceMapper proveDeviceMapper;


    @Override
    @DS("mysqltiprove")
    public RestResult<IPage<HandleProve>> page(HandleProvePageDto dto) {
        Page<HandleProve> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<HandleProve> wrapper = new LambdaQueryWrapper<>();
        if (dto.getHandleProveTitle() != null) {
            wrapper.like(HandleProve::getHandleProveTitle, dto.getHandleProveTitle());
        }
        wrapper.orderByDesc(HandleProve::getId);
        IPage<HandleProve> iPage = handleProveMapper.selectPage(page, wrapper);
        return RestResult.ok(iPage);
    }

    @Override
    @DS("mysqltiprove")
    public RestResult<List<HandleProve>> list() {
        LambdaQueryWrapper<HandleProve> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(HandleProve::getId, HandleProve::getHandleProveTitle);
        List<HandleProve> handleProves = handleProveMapper.selectList(wrapper);
        return RestResult.ok((handleProves == null || handleProves.isEmpty()) ? Collections.emptyList() : handleProves);
    }

    @Override
    @DS("mysqltiprove")
    public RestResult<Boolean> add(HandleProve prove) {
        if (prove.getId() != null) {
            updateProve(prove);
        } else {
            addProve(prove);
        }
        return RestResult.ok(Boolean.TRUE);
    }

    private void updateProve(HandleProve prove) {
        HandleProve handleProve = handleProveMapper.selectById(prove.getId());
        if (handleProve == null) {
            return;
        }
        handleProve.setHandleProveNotice(prove.getHandleProveNotice());
        handleProve.setHandleProveTitle(prove.getHandleProveTitle());
        handleProveMapper.updateById(handleProve);
        handleDevice(handleProve);
    }

    private void handleDevice(HandleProve handleProve) {
        try {
            LambdaQueryWrapper<ProveDevice> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProveDevice::getHandleProveId, handleProve.getId()).eq(ProveDevice::getIsDeleted, Boolean.FALSE);
            List<ProveDevice> devices = proveDeviceMapper.selectList(wrapper);
            if (devices != null && devices.size() > 0) {
                devices.stream().forEach(item -> {
                    item.setHandleProveTitle(handleProve.getHandleProveTitle());
                    item.setHandleProveNotice(handleProve.getHandleProveNotice());
                    proveDeviceMapper.updateById(item);

                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void addProve(HandleProve prove) {
        prove.setCreateTime(new Date());
        handleProveMapper.insert(prove);
    }

    @Override
    @DS("mysqltiprove")
    public RestResult del(Long id) {
        HandleProve handleProve = handleProveMapper.selectById(id);
        if (handleProve == null) {
            return RestResult.error("数据不存在/已删除");
        }
        handleProveMapper.deleteById(handleProve);
        return RestResult.ok(Boolean.TRUE);
    }

    @Override
    @DS("mysqltiprove")
    public RestResult<IPage<ProveDeviceVo>> pageDevice(ProveDevicePageDto dto) {
        Page<ProveDevice> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<ProveDevice> wrapper = new LambdaQueryWrapper<>();
        if (dto.getAddress() != null && !"".equals(dto.getAddress().trim())) {
            wrapper.like(ProveDevice::getAddress, dto.getAddress());
        }
        if (dto.getSn() != null && !"".equals(dto.getSn().trim())) {
            wrapper.like(ProveDevice::getSn, dto.getSn());
        }
        if (dto.getStartTime() != null) {
            wrapper.ge(ProveDevice::getCreateTime, dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            wrapper.le(ProveDevice::getCreateTime, dto.getEndTime());
        }
        wrapper.orderByDesc(ProveDevice::getId);
        IPage<ProveDevice> iPage = proveDeviceMapper.selectPage(page, wrapper);
        IPage<ProveDeviceVo> convert = iPage.convert(t -> {
            ProveDeviceVo item = BeanUtil.copyProperties(t, ProveDeviceVo.class);
            return item;
        });
        return RestResult.ok(convert);
    }

    @Override
    @DS("mysqltiprove")
    public RestResult addDevice(ProveDeviceDto dto) {
        if (dto.getHandleProveId() == null || dto.getSn() == null || dto.getAddress() == null) {
            return RestResult.error("参数有误");
        }
        LambdaQueryWrapper<ProveDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProveDevice::getSn, dto.getSn()).eq(ProveDevice::getIsDeleted, Boolean.FALSE);
        ProveDevice device = proveDeviceMapper.selectOne(wrapper);
        if (device == null) {
            device = BeanUtil.copyProperties(dto, ProveDevice.class);
            HandleProve handleProve = handleProveMapper.selectById(dto.getHandleProveId());
            if (handleProve != null) {
                device.setHandleProveTitle(handleProve.getHandleProveTitle());
                device.setHandleProveNotice(handleProve.getHandleProveNotice());
            }
            device.setIsDeleted(Boolean.FALSE);
            device.setCreateTime(new Date());
            proveDeviceMapper.insert(device);
        } else {
            device.setAddress(dto.getAddress());
            device.setHandleProveId(dto.getHandleProveId());
            HandleProve handleProve = handleProveMapper.selectById(dto.getHandleProveId());
            if (handleProve != null) {
                device.setHandleProveTitle(handleProve.getHandleProveTitle());
                device.setHandleProveNotice(handleProve.getHandleProveNotice());
            }
            device.setZhang(dto.getZhang());
            device.setSn(dto.getSn());
            proveDeviceMapper.updateById(device);
        }
        return RestResult.ok();
    }

    @Override
    @DS("mysqltiprove")
    public RestResult delDevice(Long id) {
        ProveDevice device = proveDeviceMapper.selectById(id);
        if (device == null) {
            return RestResult.error("数据不存在/已删除");
        }
        proveDeviceMapper.deleteById(device);
        return RestResult.ok();
    }


}
