package com.fjs.scenic.service.system.impl;

import java.util.Date;
import java.util.List;

//import com.fjs.scenic.utils.UserUtils;
import com.fjs.scenic.entity.user.UserStatistical;
import com.fjs.scenic.mapper.user.UserStatisticalMapper;
import com.fjs.scenic.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.mapper.system.ManageMapper;
import com.fjs.scenic.mapper.system.ManageRoleMapper;
import com.fjs.scenic.service.common.impl.BaseServiceImpl;
import com.fjs.scenic.service.system.ManageService;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;

/**
 * This class is used for ...
 * 管理员
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@Service
public class ManageServiceImpl extends BaseServiceImpl<Manage> implements ManageService {

    @Autowired
    private ManageMapper manageMapper;
    @Autowired
    private ManageRoleMapper manageRoleMapper;
    @Autowired
    private UserStatisticalMapper userStatisticalMapper;
    /**
     * 添加管理员
     *
     * @param manage
     * @return
     */
    public ReturnResult save(Manage manage) {

        //判断参数完整
        if (manage.getName() == null || manage.getName().equals("") ||
                manage.getAccount() == null || manage.getAccount().equals("") ||
                manage.getPassword() == null || manage.getPassword().equals("") ||
                manage.getPhone() == null || manage.getPhone().equals("")) {
            return ReturnResult.build(Cons.RESULT_ERR, "参数不完整！");
        }

        //根据账户判断是否已注册
        Manage m = manageMapper.selectByAccount(manage.getAccount());
        if (null != m) {
            return ReturnResult.build(Cons.RESULT_ERR, "账户名已存在！");
        }
        //密码加密，时间设置
        manage.setPassword(manage.getPassword());
        manage.setCreateTime(new Date());
        manage.setUpdateTime(new Date());

        int row = manageMapper.insertSelective(manage);
        if (row > 0) {
            return new ReturnResult(200, "ok", null);
        }
        return ReturnResult.build(Cons.RESULT_ERR, "添加失败！");
    }

    /**
     * 删除管理员
     *
     * @param id 管理员ID
     * @return
     */
    public ReturnResult delManage(int id) {
        manageMapper.deleteByPrimaryKey(id);
        manageRoleMapper.deleteByManageId(id);
        return ReturnResult.ok();
    }

    /**
     * 保存管理员的角色
     *
     * @param id    管理员ID
     * @param roles 角色ID（逗号拼接）
     * @return
     */
    public ReturnResult saveManageRole(Integer id, String roles) {
        String[] roleIds = roles.split(",");
        manageRoleMapper.deleteByManageId(id);
        manageRoleMapper.insertManageRoleList(roleIds, id);
        return ReturnResult.ok();
    }

    public ReturnResult get(Integer id) {
        Manage manage = manageMapper.selectByPrimaryKey(id);
        manage.setPassword(null);
        return ReturnResult.ok(manage);
    }

    @Override
    public Manage getMangerByAccount(String username) {
        Manage manager = manageMapper.selectByAccount(username);
        if (manager != null && !manager.getStatus().equals(Cons.MANAGE_STATUS.NORMAL)) {
            return null;
        }
        return manager;
    }

    @Override
    public List<Manage> selectRepet(Manage manage) {
        return manageMapper.selectRepet(manage);
    }

    @Override
    public List<Manage> page1(Manage record) {
        return manageMapper.selectAll1(record);
    }

    /**
     * 获取具有领导决策管理员的信息
     *
     * @return
     */
    @Override
    public Manage selectLDJCManage() {
        List<Manage> manages = manageMapper.selectLDJCManage();
        return manages.size() == 0 ? null : manages.get(0);
    }

    /**
     * 管理账户冻结 解冻
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int freezeUnfreeza(Integer id, String status) {
        Manage manage = new Manage();
        manage.setId(id);
        manage.setStatus(status);
        return manageMapper.updateByPrimaryKeySelective(manage);
    }

    /**
     * 验证密码
     * @param account
     * @param oldPWD 要验证的密码
     * @return
     */
    @Override
    public boolean verifyPWD(String account, String oldPWD) {
        Manage manage = new Manage();
        manage.setAccount(account);
        manage.setPassword(oldPWD);
        return manageMapper.selectAll(manage).size() == 0 ? false : true;
    }

    /**
     * 修改密码
     * @param account
     * @param newPWD 新密码
     * @return
     */
    @Override
    public boolean updatePWD(String account, String newPWD) {
        Manage manage = new Manage();
        manage.setAccount(account);
        manage.setPassword(newPWD);
        return manageMapper.updateByAccountKeySelective(manage) == 1 ? true : false;
    }

    @Override
    public int add1(Manage manage) {
        String[] pids = manage.getPermissions().split(",");
        manage.setPassword(Md5Utils.MD5(manage.getPassword()));
        int add = manageMapper.add(manage);
        if(add>0){
            Integer id = manageMapper.queryByPhone(manage.getPhone());
            for (String permissonId : pids) {
                UserStatistical us = new UserStatistical();
                us.setUserId(id);
                us.setStatiscalPermissionsId(Integer.valueOf(permissonId));
                userStatisticalMapper.insertSelective(us);
            }
        }
        return 1;
    }

    @Override
    public int update1(Manage manage) {
        Integer id = manage.getId();
        userStatisticalMapper.deleteByUserId(id);
        String[] pids = manage.getPermissions().split(",");
        for (String permissonId : pids) {
            UserStatistical us = new UserStatistical();
            us.setUserId(id);
            us.setStatiscalPermissionsId(Integer.valueOf(permissonId));
            userStatisticalMapper.insertSelective(us);
        }
        int update = manageMapper.update(manage);
        if(null != manage.getPassword()){
            if(update >0){
                return manageMapper.updatePwd(id,Md5Utils.MD5(manage.getPassword()));
            }
        }
        return 0;
    }

    @Override
    public int delete1(Integer id) {
        return manageMapper.delete(id);
    }

    @Override
    public List<Manage> queryAll1() {
        return manageMapper.queryAll();
    }

    @Override
    public Manage queryOne1(Integer id) {
        return manageMapper.queryOne(id);
    }

    @Override
    public Integer queryByPhone1(String phone) {
        return manageMapper.queryByPhone(phone);
    }

    @Override
    public int updateStatus1(Integer id, String status) {
        return manageMapper.updateStatus(id,status);
    }

    @Override
    public Manage getManeger(String account, String password) {
        return manageMapper.getManeger(account,password);
    }

    @Override
    public Integer queryByAccount(String account) {
        return manageMapper.queryByAccount(account);
    }
}
