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

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.system.SysPostMapper;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.SysPost;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysPostService;
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;

/**
 * Created by dell on 2017-07-07.
 */
@Service("SysPostService")
public class SysPostServiceImpl implements SysPostService {

    @Autowired
    public SysPostMapper sysPostMapper;



    int num = 0;
    String result=Type.FAIL;
    Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();//gson json的三方框架 数据传递格式

    @Override
    public SysPost queryById(String id) {
        return  sysPostMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<SysPost> queryAll() {
        List<SysPost> list =  sysPostMapper.selectAll();
        return list;
    }

    @Override
    public String query(int page, int rows, String postName) {
            PageHelper.startPage(page, rows);
            List<SysPost> list = new ArrayList<SysPost>();
            list = sysPostMapper.select(postName);
            PageInfo<SysPost> pageInfo = new PageInfo<>(list);
            result = gson.toJson(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, pageInfo));
            return result;
    }

    @Override
    public String save(String id, String postName, String postDesc, String userId) {
        SysPost sysPost;
        if ( id.equals("")||id.equals(null)) {
            sysPost = new SysPost();
            sysPost.setPostName(postName);
            sysPost.setPostDesc(postDesc);
            sysPost.setfId("Vue测试");
            sysPost.setAddId(userId);
            sysPost.setAddTime(new Date());
            sysPost.setCancelFlag("0");
            num = sysPostMapper.insert(sysPost);

//
//            Group g = identityService.newGroup(postName);
//            g.setName(postDesc);
//            g.setType("assignment");
//            identityService.saveGroup(g);


            result = Tools.moreThanZero(num);
        } else {
            sysPost = sysPostMapper.selectByPrimaryKey(id);
            sysPost.setPostName(postName);
            sysPost.setPostDesc(postDesc);
            sysPost.setModifyId(userId);
            sysPost.setModifyTime(new Date());
            num = sysPostMapper.updateByPrimaryKey(sysPost);

//
//            Group g = identityService.createGroupQuery().groupId(postName).singleResult();
//            g.setName(postDesc);
//
//            identityService.saveGroup(g);


            result = Tools.moreThanZero(num);
        }
        result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, result));
        return result;
    }

    @Override
    public String remove(String id,String userId) {
        SysPost sysPost = sysPostMapper.selectByPrimaryKey(id);
        sysPost.setCancelId(userId);
        sysPost.setCancelTime(new Date());
        sysPost.setCancelFlag("9");
        num = sysPostMapper.updateByPrimaryKey(sysPost);

//
//        Group g = identityService.createGroupQuery().groupId(sysPost.getPostName()).singleResult();
//        identityService.deleteGroup(g.getId());
        result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, result));
        return result;
    }
}
