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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.system.SysPostMapper;
import com.th.demo.mapping.system.SysUserMapper;
import com.th.demo.mapping.system.SysUserDeptPostMapper;
import com.th.demo.model.system.*;

import com.th.demo.service.system.SysUserDeptPostService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017-07-10.
 */
@Service("SysUserDeptPostService")
public class SysUserDeptPostServiceImpl implements SysUserDeptPostService {

    @Autowired
    public SysUserDeptPostMapper sysUserdeptpostMapper;

    @Autowired
    public SysUserMapper sysUserMapper;
    @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 List<SysUserDeptPostShow> query(String userId) {
        List<SysUserDeptPostShow> list = sysUserdeptpostMapper.select(userId);
        return list;
    }

    @Override
    public SysUserDeptPost  queryById(String id) {
        SysUserDeptPost udp = sysUserdeptpostMapper.selectByPrimaryKey(id);
        return udp;
    }

    @Override
    public int modify(SysUserDeptPost udp) {
        num = 0;
        num = sysUserdeptpostMapper.updateByPrimaryKey(udp);
        return num;
    }

    @Override
    public int add(SysUserDeptPost udp) {
        num = 0;
        num = sysUserdeptpostMapper.insert(udp);
        return num;
    }

    @Override
    public int remove(String id) {
        num = 0;
        num = sysUserdeptpostMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public List<SysUser> queryByDeptPost(String deptId, String postId) {
        String[] strDeptId = deptId.split(",");
        Integer[] intDeptId = new Integer[strDeptId.length]  ;
        for(int i =0;i<intDeptId.length;i++){

            intDeptId[i] = Integer.parseInt(strDeptId[i]);
        }


        String[] strPostId = postId.split(",");
        Integer[] intPostId = new Integer[strPostId.length]  ;
        for(int i =0;i<intPostId.length;i++){

            intPostId[i] = Integer.parseInt(strPostId[i]);
        }


        Map<String, Integer[]> map= new HashMap<String, Integer[]>();
        map.put("deptId",intDeptId);
        map.put("postId",intPostId);

        List<SysUser> list = sysUserMapper.selectByDeptPost(map);
        return list;
    }

    @Override
    public String save(String userId, String deptId, String postId, String addId) {
        SysUserDeptPost udp = new SysUserDeptPost();
        udp.setUserId(userId);
        udp.setDeptId(deptId);
        udp.setPostId(postId);
        udp.setAddId(addId);
        udp.setAddTime(new Date());
        udp.setCancelFlag("0");
        num = sysUserdeptpostMapper.insert(udp);
        result = Tools.moreThanZero(num);

        return result;
    }

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

        result = Tools.moreThanZero(num);
        return result;
    }
}
