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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
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.SysUserMapper;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.ResultJSON;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysUserService;
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("SysUserService")//必须写  Service的名字
public class SysUserServiceImpl implements SysUserService {

    @Autowired//自动注入Mapper
    public SysUserMapper sysUserMapper;

    int num = 0;
    String result = Type.FAIL;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();

    @Override
    public String query(String userName, String userDesc) {
        List<SysUser> list = sysUserMapper.selectUser(userName, userDesc);
        return JSON.toJSONString(list);
    }

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

        if (sysUser.getId() == null || sysUser.getId().equals("")) {
            sysUser.setAddId(userId);
            sysUser.setAddTime(new Date());
            sysUser.setCancelFlag("0");
            sysUser.setBelongId(belongId);
            num = sysUserMapper.insert(sysUser);

        } else {
            sysUser.setModifyId(userId);
            sysUser.setModifyTime(new Date());
            num = sysUserMapper.updateByPrimaryKey(sysUser);

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


    }


    @Override
    public String remove(String id, String userId) throws Exception {
        try {
            num = 0;
            result = Type.FAIL;
            SysUser user = sysUserMapper.selectByPrimaryKey(id);
            user.setCancelFlag("9");
            user.setCancelId(userId);
            user.setCancelTime(new Date());
            num = sysUserMapper.updateByPrimaryKey(user);
            result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, result));
            return result;
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public String query(int page, int rows, String userDesc, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<SysUser> list = new ArrayList<SysUser>();
        if (userId.equals("1")) {
            list = sysUserMapper.selectByAdmin(userDesc);
        } else {
            list = sysUserMapper.select(userDesc, belongId);
        }
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String updatePassword(String userId, String oldPassword, String newPassword) {
        SysUser user = sysUserMapper.selectByPrimaryKey(userId);

        if (user != null) {

            if (user.getPassword().equals(oldPassword)) {
                user.setPassword(newPassword);
                user.setModifyId(userId);
                user.setModifyTime(new Date());
                num = sysUserMapper.updateByPrimaryKey(user);

                result = Tools.moreThanZero(num);
            } else {
                result = "密码不正确";
            }
        } else {
            result = "未找到用户";
        }

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

    @Override
    public String queryByDept(String deptId) {

        List<SysUser> list = sysUserMapper.selectByDept(deptId);
        result = JSON.toJSONString(new ResultJSON(ResponseCodeMsg.CODE_OK, ResponseCodeMsg.MSG_OK, list));
        return result;
    }

    @Override
    public String queryByUserId(String userId) {
        return sysUserMapper.selectByPrimaryKey(userId).getUserDesc();
    }


}
