package com.th.demo.service.impl.maint;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.UserWareMapper;
import com.th.demo.mapping.maint.WareMapper;
import com.th.demo.model.maint.Material;
import com.th.demo.model.maint.UserWare;
import com.th.demo.model.maint.Ware;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.UserWareService;
import com.th.demo.service.maint.WareService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("UserWareService")
public class UserWareServiceImpl implements UserWareService {

    @Autowired
    public WareMapper wareMapper;
    @Autowired
    public UserWareMapper userWareMapper;
    String result = Type.FAIL;
    int num = 0;


    @Override
    public String queryByUserId(String userId, String belongId) {
        List<String> list = userWareMapper.selectByUserIdForUserWare(userId, belongId);
        result = JSONTools.toString(list);
        return result;
    }

    @Override
    public String save(String selectUserId, String selectWareId, String userId, String belongId) {
        num = userWareMapper.deleteByUserIdBelongId(selectUserId, belongId);
        String[] strArr = JSON.parseObject(selectWareId, new TypeReference<String[]>() {
        });
        for (int i = 0; i < strArr.length; i++) {
            UserWare userWare = new UserWare();
            userWare.setUserId(selectUserId);
            userWare.setWareId(strArr[i]);
            userWare.setBelongId(belongId);
            userWare.setAddId(userId);
            userWare.setAddTime(new Date());
            userWare.setCancelFlag("0");
            num = userWareMapper.insert(userWare);
        }
        return Tools.moreThanZeroResultJSON(num);
    }
}
