package com.sy.coinage.workshop.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.google.common.io.BaseEncoding;
import com.sy.coinage.app.req.vo.CheckDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.sy.coinage.workshop.dao.CheckDetailDao;
import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.ICheckDetailService;

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

/**
 * -ServiceImpl
 */
@Service
public class CheckDetailServiceImpl extends ServiceImpl<CheckDetailDao, CheckDetail> implements ICheckDetailService {
    @Override
    public CheckDetail getByRfidSerialNumber(String rfid, String serialNumber, Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        queryWrapper.eq(CheckDetail::getRfid, rfid);
        queryWrapper.eq(CheckDetail::getSerialNumber, serialNumber);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<CheckDetailVO> getInitList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    @Override
    public List<CheckDetailVO> getShopReceiveList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        queryWrapper.isNotNull(CheckDetail::getShopReceiveList);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    @Override
    public List<CheckDetailVO> getShopInProList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        queryWrapper.isNotNull(CheckDetail::getShopInProList);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    @Override
    public List<CheckDetailVO> getShopOutMachineList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        queryWrapper.isNotNull(CheckDetail::getShopOutMachineList);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    @Override
    public List<CheckDetailVO> getShopOutList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckDetail::getCheckId, checkId);
        queryWrapper.isNotNull(CheckDetail::getShopOutList);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    @Override
    public List<CheckDetailVO> getOpList(Integer checkId) {
        LambdaQueryWrapper<CheckDetail> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(CheckDetail::getCheckId, checkId);
//        queryWrapper.isNotNull(CheckDetail::getShopOutList);
        List<CheckDetail> list = this.list(queryWrapper);
        return this.conver(list);
    }

    private List<CheckDetailVO> conver(List<CheckDetail> list) {
        List<CheckDetailVO> resp = new ArrayList<>();
        for (CheckDetail model : list) {
            CheckDetailVO vo = new CheckDetailVO();
            BeanUtils.copyProperties(model, vo);
            vo.setCrownArray(JSON.parseArray(model.getWareInitList(), String.class));
            resp.add(vo);
        }
        return resp;
    }

}
