package com.shinsoft.sto.service.impl.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shinsoft.sto.mapper.main.StackMapper;
import com.shinsoft.sto.model.main.Stack;
import com.shinsoft.sto.model.system.ResponseCodeMsg;
import com.shinsoft.sto.model.system.ResultJSON;
import com.shinsoft.sto.service.main.StackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("StackService")
public class StackServiceImpl implements StackService {

    @Autowired
    private StackMapper stackMapper;

    @Override
    public ResultJSON query(int page, int rows, String query, String queryWare, String userId, String belongId) {
        Page<Stack> pricePage = new Page<>(page, rows);
        Page<Stack> list = stackMapper.select(pricePage, query, queryWare, belongId);
        return ResultJSON.success(list);
    }

    @Override
    public ResultJSON save(String json, String userId, String belongId) {
        JSONObject object = JSON.parseObject(json);
//        String wareName = object.getJSONObject("ware").getString("name");
        String wareName = object.getString("wareId");
        Stack stack = JSON.parseObject(json, new TypeReference<Stack>() {
        });
        stack.setWareId(wareName);

        int num;
        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);
            if (num > 0) {
                return ResultJSON.success("新增成功");
            }
        } else {
            stack.setModifyId(userId);
            stack.setModifyTime(new Date());
            num = stackMapper.updateById(stack);
            if (num > 0) {
                return ResultJSON.success("更新成功");
            }
        }
        return ResultJSON.error(ResponseCodeMsg.CODE_FAIL, "操作失败，未执行成功");
    }

    @Override
    public ResultJSON remove(String id, String userId) {
        Stack stack = stackMapper.selectByPrimaryKey(id);
        if (stack == null) {
            return ResultJSON.error(ResponseCodeMsg.CODE_NOT_FOUND, "垛位不存在");
        }
        stack.setCancelFlag("9");
        stack.setCancelId(userId);
        stack.setCancelTime(new Date());
        int num = stackMapper.updateById(stack);
        if (num > 0) {
            return ResultJSON.success("删除成功");
        }
        return ResultJSON.error(ResponseCodeMsg.CODE_FAIL, "操作失败，未执行成功");
    }

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