package com.yutou.tools.Tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.ToolsPasswordDao;
import com.yutou.tools.mybatis.dao.ToolsPasswordTypeDao;
import com.yutou.tools.mybatis.model.ToolsPassword;
import com.yutou.tools.mybatis.model.ToolsPasswordExample;
import com.yutou.tools.mybatis.model.ToolsPasswordType;
import com.yutou.tools.mybatis.model.ToolsPasswordTypeExample;
import com.yutou.tools.utils.AESTools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/tools/password/")
public class PasswordManager {

    @Resource
    ToolsPasswordDao passwordDao;
    @Resource
    ToolsPasswordTypeDao passwordTypeDao;

    @ResponseBody
    @RequestMapping(value = "get/list.do",method = RequestMethod.GET)
    public String getPasswordList(String type){
        JSONObject json=new JSONObject();
        ToolsPasswordExample example=new ToolsPasswordExample();
        example.createCriteria().andTypeEqualTo(Integer.parseInt(type));
        List<ToolsPassword> passwords=passwordDao.selectByExample(example);
        for (ToolsPassword password : passwords) {
            password.setPassword("*****");
        }
        json.put("code",0);
        json.put("msg","ok");
        json.put("data", JSONArray.toJSON(passwords));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "get/password.do",method = RequestMethod.GET)
    public String getPassword(String id){
        JSONObject json=new JSONObject();
        ToolsPassword password=passwordDao.selectByPrimaryKey(Integer.parseInt(id));
        json.put("code",0);
        json.put("data",AESTools.decrypt(password.getPassword()));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "type/get/list.do",method = RequestMethod.GET)
    public String getPasswordType(){
        JSONObject json=new JSONObject();
        List<ToolsPasswordType> toolsPasswordTypes=passwordTypeDao.selectByExample(new ToolsPasswordTypeExample());
        json.put("code",0);
        json.put("msg","ok");
        json.put("data", JSONArray.toJSON(toolsPasswordTypes));
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "type/set/add.do",method = RequestMethod.POST)
    public String addPasswordType(String type){
        JSONObject json=new JSONObject();
        try{
            ToolsPasswordType passwordType=new ToolsPasswordType();
            passwordType.setTitle(type);
            int code=passwordTypeDao.insert(passwordType);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "set/add.do",method = RequestMethod.POST)
    public String addPassword(String title,String username,String password,String url,String info,String type){
        JSONObject json=new JSONObject();
        try{
            if(StringUtils.isEmpty(title)||StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(type)){
                json.put("code",-1);
                json.put("msg","有参数为空");
                return json.toJSONString();
            }
            ToolsPassword toolsPassword=new ToolsPassword();
            toolsPassword.setTitle(title);
            toolsPassword.setUsername(username);
            toolsPassword.setPassword(AESTools.encrypt(password));
            toolsPassword.setType(Integer.parseInt(type));
            if(url!=null){
                toolsPassword.setUrl(url);
            }
            if(info!=null){
                toolsPassword.setInfo(info);
            }
            int code=passwordDao.insert(toolsPassword);
            json.put("code",0);
            json.put("msg","添加结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "set/update.do",method = RequestMethod.POST)
    public String updatePassword(String title,String username,String password,String url,String info,String id){
        JSONObject json=new JSONObject();
        try{
            ToolsPassword toolsPassword=passwordDao.selectByPrimaryKey(Integer.parseInt(id));
            if(!StringUtils.isEmpty(title)){
                toolsPassword.setTitle(title);
            }
            if(!StringUtils.isEmpty(username)){
                toolsPassword.setUsername(username);
            }
            if(!StringUtils.isEmpty(password)){
                toolsPassword.setPassword(AESTools.encrypt(password));
            }
            if(!StringUtils.isEmpty(url)){
                toolsPassword.setUrl(url);
            }
            if(!StringUtils.isEmpty(info)){
                toolsPassword.setInfo(info);
            }
            int code=passwordDao.updateByPrimaryKey(toolsPassword);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "type/set/update.do",method = RequestMethod.POST)
    public String updatePasswordType(String type,String id){
        JSONObject json=new JSONObject();
        try {
            ToolsPasswordType passwordType=passwordTypeDao.selectByPrimaryKey(Integer.parseInt(id));
            if(!StringUtils.isEmpty(type)){
                passwordType.setTitle(type);
            }
            int code=passwordTypeDao.updateByPrimaryKey(passwordType);
            json.put("code",0);
            json.put("msg","修改结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "set/remove.do",method = RequestMethod.POST)
    public String removePassword(String id){
        JSONObject json=new JSONObject();
        try {
            int code=passwordDao.deleteByPrimaryKey(Integer.parseInt(id));
            json.put("code",0);
            json.put("msg","删除结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
    @ResponseBody
    @RequestMapping(value = "type/set/remove.do",method = RequestMethod.POST)
    public String removePasswordType(String id){
        JSONObject json=new JSONObject();
        try {
            ToolsPasswordExample example=new ToolsPasswordExample();
            example.createCriteria().andTypeEqualTo(Integer.parseInt(id));
            int passwordCode=passwordDao.deleteByExample(example);
            System.out.println("删除结果："+passwordCode);
            int code=passwordTypeDao.deleteByPrimaryKey(Integer.parseInt(id));
            json.put("code",0);
            json.put("msg","删除结果:"+code);
        }catch (Exception e){
            e.printStackTrace();
            json.put("code",-1);
            json.put("msg",e.getMessage());
        }
        return json.toJSONString();
    }
}
