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.MaterialMapper;
import com.th.demo.mapping.maint.MaterialMapper;
import com.th.demo.model.maint.Customer;
import com.th.demo.model.maint.Material;
import com.th.demo.model.maint.Material;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.MaterialService;
import com.th.demo.service.maint.MaterialService;
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("MaterialService")
public class MaterialServiceImpl implements MaterialService {

    @Autowired//自动注入Mapper
    public MaterialMapper materialMapper;
    String result = Type.FAIL;
    int num = 0;

    @Override
    public String query(int page, int rows, String material,String standard, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<Material> list = new ArrayList<Material>();

        list = materialMapper.select(  material,  standard, belongId);

        PageInfo<Material> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        result = Type.FAIL;
        Material material = JSON.parseObject(json, new TypeReference<Material>() {
        });

        if (material.getId() == null || material.getId().equals("")) {
            // 防止维护重复，增加信息校验
            List<Material> materialList = materialMapper.selectListByNameStandard(material.getName(), material.getStandard(), belongId);
            if (materialList.size() >= 1){
                return JSONTools.toString(1,material.getName()+material.getStandard()+"已存在，请勿重复维护");
            }
            material.setAddId(userId);
            material.setAddTime(new Date());
            material.setModifyId(userId);
            material.setModifyTime(new Date());
            material.setCancelFlag("0");
            material.setBelongId(belongId);
            num = materialMapper.insert(material);
        } else {
            List<Material> materialList = materialMapper.selectListByNameStandard(material.getName(), material.getStandard(), belongId);
            if (materialList.size() == 1 && !material.getId().equals(materialList.get(0).getId())){
                return JSONTools.toString(1,material.getName()+material.getStandard()+"已存在，请勿重复维护");
            } else if (materialList.size() > 1) {
                return JSONTools.toString(1,material.getName()+material.getStandard()+"已存在，请勿重复维护");
            }
            material.setModifyId(userId);
            material.setModifyTime(new Date());
            num = materialMapper.updateByPrimaryKey(material);
        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }


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

    }

    @Override
    public String queryMaterialName(String userId, String belongId) {

        List<Material> list = new ArrayList<Material>();
        list = materialMapper.selectMaterialName( belongId);
        result = JSONTools.toString(list);
        return result;
    }
}
