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

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysDicService;
import com.th.demo.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.th.demo.mapping.system.SysDicMapper;
import com.th.demo.model.system.SysDic;



@Service("SysDicService")
public class SysDicServiceImpl implements SysDicService {
    @Autowired
    public SysDicMapper sysDicMapper;
    int num = 0;
    String result=Type.FAIL;
    @Override
    public String query(int page, int rows, String dicType) {
            PageHelper.startPage(page, rows);
            List<SysDic> list = new ArrayList<SysDic>();
            list = sysDicMapper.select(dicType);
            PageInfo<SysDic> pageInfo = new PageInfo<>(list);
            result = JSONTools.toString(pageInfo);
            return result;
    }

    @Override
    public String save(String json,  String userId) {
        SysDic sysDic = JSON.parseObject(json,SysDic.class);
        List<SysDic> sysDicList = sysDicMapper.selectByDic(sysDic);
        if (sysDicList.size() > 0){
            return JSONTools.toString(1,"该类型下该编码或者该名称已存在");
        }
        if (sysDic.getId()==null||sysDic.getId().equals("")) {
            sysDic.setAddId(userId);
            sysDic.setAddTime(new Date());
            num = sysDicMapper.insert(sysDic);
            result =JSONTools.toString(num);
        } else {
            sysDic.setModifyId(userId);
            sysDic.setModifyTime(new Date());
            num = sysDicMapper.updateByPrimaryKey(sysDic);
            result = JSONTools.toString(num);
        }
        return result;
    }

    @Override
    public String remove(String id,String userId) {
        num = sysDicMapper.deleteByPrimaryKey(id);
        result = JSONTools.toString(num);
        return result;
    }

    @Override
    public String queryByType(int page, int rows, String dicType) {
        PageHelper.startPage(page, rows);
        List<SysDic> list = new ArrayList<SysDic>();
        list = sysDicMapper.selectByDicType(dicType);
        PageInfo<SysDic> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

}
