package com.shinsoft.sto.service.impl;

import com.shinsoft.frameworkapi.model.SysDepartment;
import com.shinsoft.frameworkapi.model.SysUser;
import com.shinsoft.sto.model.Onhand;
import com.shinsoft.sto.model.PickRecord;
import com.shinsoft.sto.mapper.PickRecordMapper;
import com.shinsoft.sto.model.Stocktaking;
import com.shinsoft.sto.model.TransRecord;
import com.shinsoft.sto.service.PickRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import org.springframework.beans.factory.annotation.Autowired;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResultJSON;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 领料记录表 服务实现类
 * </p>
 *
 * @author system
 * @since 2025-01-13
 */
@Service
public class PickRecordServiceImp extends ServiceImpl<PickRecordMapper, PickRecord> implements PickRecordService {


    ResultJSON resultJSON;

    @Autowired
    PickRecordMapper pickRecordMapper;

    @Override
    public ResultJSON query(int page, int rows, String params) {
        JSONObject jsonObject = JSON.parseObject(params);
        Page<PickRecord> pickRecordPage = new Page<>(page, rows);
//        QueryWrapper<PickRecord> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("cancel_flag", "0");
//        queryWrapper.orderByDesc("add_time");
        IPage<PickRecord> list = pickRecordMapper.queryList(pickRecordPage, jsonObject);
        resultJSON = JSONTools.toResultJSON(list);
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        PickRecord pickRecord = pickRecordMapper.selectByPK(id);
        resultJSON = JSONTools.toResultJSON(pickRecord);
        return resultJSON;
    }


    @Override
    public ResultJSON save(String userId, String json) {
        Date date = new Date();
        PickRecord pickRecord = JSON.parseObject(json, new TypeReference<PickRecord>() {
        });
        if (StringUtils.isEmpty(pickRecord.getId())) {
            pickRecord.setCancelFlag("0");
            pickRecord.setAddId(userId);
            pickRecord.setAddTime(date);
            pickRecordMapper.insert(pickRecord);
        } else {
            pickRecord.setModifyId(userId);
            pickRecord.setModifyTime(date);
            pickRecordMapper.updateById(pickRecord);
        }

        resultJSON = JSONTools.toResultJSON(pickRecord);
        return resultJSON;
    }

    @Override
    public ResultJSON remove(String userId, String id) {
        Date date = new Date();
        PickRecord pickRecord = pickRecordMapper.selectById(id);
        pickRecord.setCancelFlag("1");
        pickRecord.setCancelId(userId);
        pickRecord.setCancelTime(date);
        pickRecordMapper.updateById(pickRecord);
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

    @Override
    public ResultJSON removeBatch(String userId, String ids) {
        Date date = new Date();
        List<String> list = JSON.parseObject(ids, new TypeReference<List<String>>() {
        });
        PickRecord pickRecord;
        for (String id : list) {
            pickRecord = pickRecordMapper.selectById(id);
            pickRecord.setCancelFlag("1");
            pickRecord.setCancelId(userId);
            pickRecord.setCancelTime(date);
            pickRecordMapper.updateById(pickRecord);
        }
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }


}
