package com.xy.service;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yitter.idgen.YitIdHelper;
import com.xy.dto.DevicePartDto;
import com.xy.entity.DevicePart;
import com.xy.entity.DeviceSysinfo;
import com.xy.mapper.DevicePartMapper;
import com.xy.utils.Emptys;
import com.xy.utils.MybatisPlusQuery;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

import static com.xy.utils.Beans.copy;


/**
 * <p>
 * 设备配件表 服务实现类
 * </p>
 *
 * @author lijin
 * @since 2024-02-02
 */
@Service
@AllArgsConstructor
@Api(tags = "设备配件表")
public class DevicePartServiceImpl extends ServiceImpl<DevicePartMapper, DevicePart> implements DevicePartService {

    private final DeviceSysinfoServiceImpl deviceSysinfoService;

    @ApiOperation("集合查询")
    @PostMapping("list")
    public R<List<DevicePartDto.Vo>> list(@RequestBody DevicePartDto.SelectList selectList) {
        LambdaQueryWrapper<DevicePart> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DevicePart.class).build();
        List<DevicePart> list = list(lambdaQueryWrapper);
        return R.ok(copy(DevicePartDto.Vo.class, list));
    }

    /**
     * 检查重力配置是否开启
     * @param dto
     * @return
     */
    public boolean checkWeightOpen(DevicePartDto.CheckWeightOpenDTO dto) {
        DevicePart devicePart = getOne(Wrappers.lambdaQuery(DevicePart.class).eq(DevicePart::getCode, "es").eq(DevicePart::getDeviceId, dto.getDeviceId()));
        String value = devicePart.getValue();
        JSONObject jsonObject = JSONUtil.parseObj(value);
        return  BooleanUtil.isTrue(jsonObject.getBool("is"));
    }

    @ApiOperation("添加或修改")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody List<DevicePartDto.SaveOrUpdate> saveOrUpdates) {
        LocalDateTime now = LocalDateTime.now();
        //循环处理
        saveOrUpdates.forEach(saveOrUpdate -> {
            Long deviceId = saveOrUpdate.getDeviceId();
            DevicePart devicePart = getOne(new LambdaQueryWrapper<DevicePart>()
                    .eq(DevicePart::getDeviceId, deviceId)
                    .eq(DevicePart::getCode, saveOrUpdate.getCode())
            );
            //删除
            if (saveOrUpdate.getIsRemove() && devicePart != null) {
                removeById(devicePart.getId());
                return;
            }

            String code = saveOrUpdate.getCode();
            //nfc 系统信息表更新
            if (Emptys.check(code) && "zfbNfc".equals(code)) {
                String value = saveOrUpdate.getValue();
                JSONObject jsonObject = JSONUtil.parseObj(value);
                boolean haveNfc = BooleanUtil.isTrue(jsonObject.getBool("is"));
                deviceSysinfoService.updateById(new DeviceSysinfo().setDeviceId(deviceId).setIsHaveAlipayNpad(haveNfc));
            }
            //新增
            if (devicePart == null) {
                DevicePart info = copy(DevicePart.class, saveOrUpdate)
                        .setId(YitIdHelper.nextId())
                        .setUpdateTime(now)
                        .setCreateTime(now);
                save(info);
                return;
            }
            //修改
            if (Emptys.check(saveOrUpdate.getValue())) {
                updateById(devicePart.setValue(saveOrUpdate.getValue()));
            }

        });
        return R.ok();
    }
}
