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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.AreaMapper;
import com.th.demo.mapping.maint.StackMapper;
import com.th.demo.mapping.maint.WareMapper;
import com.th.demo.model.maint.Area;
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.AreaService;
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.Date;
import java.util.List;

@Service("AreaService")
public class AreaServiceImpl implements AreaService {

    @Autowired
    public AreaMapper areaMapper;
    @Autowired
    public WareMapper wareMapper;

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

    @Override
    public String query(int page, int rows, String queryWare,String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<Area> list   = areaMapper.selectByWareId(queryWare, belongId);
        PageInfo<Area> 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);
        Area area= areaMapper.selectByPrimaryKey(object.get("id").toString());
        if (area == null){
            String warename = object.getJSONObject("ware").get("id").toString();
            System.out.println(warename+","+userId+","+belongId);
            Ware ware = wareMapper.selectByWareId(warename, userId, belongId);
            area.setWare(ware);

            String IDAREA = ware.getId().substring(1,5) + object.get("code").toString();
            area.setId(IDAREA);
            area.setAddId(userId);
            area.setAddTime(new Date());
            area.setName(object.get("name").toString());
            area.setCode(object.get("code").toString());

            area.setCancelFlag("0");

            area.setBelongId(belongId);

            num= areaMapper.insert(area);

            result = Tools.moreThanZeroResultJSON(num);

        }
        else{
            area.setName(object.get("name").toString());
            area.setCode(object.get("code").toString());
            area.setModifyId(userId);
            area.setModifyTime(new Date());
            String warename = object.getJSONObject("ware").get("id").toString();
            Ware ware = wareMapper.selectByWareId(warename, userId,   belongId);
            area.setWare(ware);
            num = areaMapper.updateByPrimaryKey(area);
            result = Tools.moreThanZeroResultJSON(num);
        }
        return result;
    }

    @Override
    public String remove(String id, String userId, String belongId) {
        Area area = areaMapper.selectByPrimaryKey(id);
        area.setCancelId(userId);
        area.setCancelTime(new Date());
        area.setCancelFlag("1");
        num = areaMapper.updateByPrimaryKey(area);
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String queryByWareId(String wareId, String userId, String belongId) {
        List<Area> list = areaMapper.selectByWareId(wareId,belongId);
        result = JSONTools.toString(list);

        return result;
    }


}
