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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.StackMapper;
import com.th.demo.mapping.maint.WareMapper;
import com.th.demo.model.maint.Stack;
import com.th.demo.model.maint.Ware;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.StackService;
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("StackService")
public class StackServiceImpl implements StackService {

    @Autowired
    public StackMapper stackMapper;
    @Autowired
    public WareMapper wareMapper;



    String result = Type.FAIL;
    int num = 0;

    @Override
    public String query(int page, int rows, String query, String queryWare, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        System.out.println(query+","+queryWare+","+belongId);
        List<Stack> list   = stackMapper.select(query, queryWare, belongId);
        PageInfo<Stack> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        result = Type.FAIL;

        JSONObject object = JSON.parseObject(json);
        String warename = object.getJSONObject("ware").get("name").toString();
        System.out.println(warename+","+userId+","+belongId);
        Ware ware = wareMapper.selectByWareId(warename, userId,   belongId);
        Stack stack = JSON.parseObject(json, new TypeReference<Stack>() {
        });
        System.out.println("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
        System.out.println(warename+","+userId+","+belongId);
        stack.setWare(ware);
        if (stack.getId() == null || stack.getId().equals("")) {
            stack.setAddId(userId);
            stack.setAddTime(new Date());
            stack.setModifyId(userId);
            stack.setModifyTime(new Date());
            stack.setCancelFlag("0");
            stack.setBelongId(belongId);
            num = stackMapper.insert(stack);
        } else {
            stack.setModifyId(userId);
            stack.setModifyTime(new Date());
            num = stackMapper.updateByPrimaryKey(stack);
        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }


    @Override
    public String remove(String id, String userId) {
        Stack stack = stackMapper.selectByPrimaryKey(id);
        stack.setCancelFlag("9");
        stack.setCancelId(userId);
        stack.setCancelTime(new Date());
        num = stackMapper.updateByPrimaryKey(stack);
        result = Tools.moreThanZeroResultJSON(num);
        return result;

    }

    @Override
    public  String queryByWareId(String wareId, String userId, String belongId) {
        List<Stack> list   = stackMapper.selectByWareId(wareId,belongId);
        result = JSONTools.toString(list);
        return result;
    }

    @Override
    public  String queryByWareId(String wareId, String areaId, String userId, String belongId) {
        if (areaId =="" ||areaId ==null){
            areaId="1";
        }
        List<Stack> list   = stackMapper.selectByWareIdAreaId(wareId, areaId, belongId);
        result = JSONTools.toString(list);
        return result;
    }
}
