package com.qxgmat.service;


import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.Tools;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.ManagerMapper;
import com.qxgmat.data.dao.entity.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * Created by GaoJie on 2017/11/1.
 */
@Service
public class ManagerService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(ManagerService.class);
    @Resource
    private ManagerMapper managerMapper;

    public Manager getByUserName(String userName){
        Manager manager = Manager.builder().username(userName).build();
        manager = one(managerMapper, manager);
        return manager;
    }

    public boolean equalsPassword(Manager manager, String password){
        logger.info("{},{}", manager.getPassword(), Tools.stringMD5(Tools.stringMD5(password)));
        return Objects.equals(manager.getPassword(), Tools.stringMD5(Tools.stringMD5(password)));
    }

    public Manager add(Manager manager){
        if(manager.getPassword() != null) manager.setPassword(Tools.stringMD5(Tools.stringMD5(manager.getPassword())));
        Manager in = getByUserName(manager.getUsername());
        if (in!=null) {
            throw new ParameterException("用户名已使用");
        }
        int result = insert(managerMapper, manager);
        manager = one(managerMapper, manager.getId());
        if(manager == null){
            throw new SystemException("用户添加失败");
        }
        return manager;
    }

    public Manager edit(Manager manager){
        Manager in = one(managerMapper, manager.getId());
        if(in == null){
            throw new ParameterException("用户不存在");
        }
        in = getByUserName(manager.getUsername());
        if (in != null && !in.getId().equals(manager.getId())){
            throw new ParameterException("用户名已使用");
        }
        if(manager.getPassword() != null) manager.setPassword(Tools.stringMD5(Tools.stringMD5(manager.getPassword())));
        int result = update(managerMapper, manager);
        return manager;
    }

    public boolean delete(Number id){
        Manager in = one(managerMapper, id);
        if(in == null){
            throw new ParameterException("用户不存在");
        }
        int result = delete(managerMapper, id);
        return result > 0;
    }

    public Manager get(Number id){
        Manager in = one(managerMapper, id);

        if(in == null){
            throw new ParameterException("用户不存在");
        }
        return in;
    }

    public Page<Manager> select(int page, int pageSize){return select(managerMapper, page, pageSize);
    }

    public Page<Manager> select(int page, int pageSize, String order, DirectionStatus direction){
        Example example = new Example(Manager.class);

        if(order.isEmpty()) order = "id";
        if (direction != null){
            switch(direction){
                case ASC:
                    example.orderBy(order).asc();
                    break;
                case DESC:
                default:
                    example.orderBy(order).desc();
            }
        } else {
            example.orderBy(order).desc();
        }
        return select(managerMapper, example, page, pageSize);
    }

    public List<Manager> select(Collection ids){
        return select(managerMapper, ids);
    }
}
