package com.nmgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nmgs.entity.ServiceAreaOperators;
import com.nmgs.mapper.OperatorsMapper;
import com.nmgs.service.IOperatorsService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PropertiesUtil;
import com.nmgs.util.PubTools;
import com.trkf.PasswordEncryption.PassWordUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class OperatorsServiceImpl implements IOperatorsService {


    @Autowired
    private OperatorsMapper operatorsMapper;

    public static Logger logger = LoggerFactory.getLogger(OperatorsServiceImpl.class);


    /**
     * 分页查找岗位数据
     * @param params
     * @return
     */
    @Override
    public Page<ServiceAreaOperators> getOperatorsList(Map<String, Object> params) {

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));

        //设置分页
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        Page<ServiceAreaOperators> page = new Page<>(pageNum,pageSize);

        page = operatorsMapper.getOperatorsList(page,params);
        //给密码解密
        List<ServiceAreaOperators> records = page.getRecords();
        if(null != records && records.size() > 0){
            for (ServiceAreaOperators record : records) {
                record.setPassWord(PassWordUtils.decrypt(record.getPassWord()));
            }
            page.setRecords(records);
        }

        return page;
    }

    /**
     * 新增或修改数据
     * @param operators
     * @return
     * @throws Exception
     */
    @Override
    public int addOperators(ServiceAreaOperators operators) throws Exception {

        //如果数据为空，直接返回
        if(null == operators){
            return 0;
        }
        //判断传回的值中，id是否有值，没有就是新增
        if(null == operators.getId()){
            //先判断工号是否已经存在，存在不让保存数据
            List<ServiceAreaOperators> operationByJobNumber = operatorsMapper.getOperationByJobNumber(operators.getJobNumber());
            if(null != operationByJobNumber && operationByJobNumber.size() > 0){
                return -2;
            }
            //查询当前数据中id的最大值,给id赋值
            Integer maxId = operatorsMapper.getMaxId();
            //如果为空则赋值成1
            if(null == maxId){
                maxId = 1;
            }else{
                maxId =  maxId+1;
            }
            //给密码加密
            operators.setPassWord(PassWordUtils.encrypt(operators.getPassWord()));
            operators.setId(maxId);
            operators.setCreateTime(new Date());

            return operatorsMapper.insert(operators);
        }else{
            return 0;
        }
    }

    @Override
    public int updateOperators(ServiceAreaOperators operators) throws Exception {

        //如果数据为空，直接返回
        if(null == operators){
            return 0;
        }
        //先判断工号是否已经存在，存在不让保存数据
        List<ServiceAreaOperators> operationByJobNumber = operatorsMapper.getOperationByJobNumber(operators.getJobNumber());
        if(null != operationByJobNumber && operationByJobNumber.size() > 0){
            if(operationByJobNumber.get(0).getId() != operators.getId()){
                return -2;
            }
        }

        //判断传回的值中，id是否有值,没有直接返回
        if(null == operators.getId()){
            return 0;
        }else{
            //给密码加密
            operators.setPassWord(PassWordUtils.encrypt(operators.getPassWord()));
            //设置修改条件，根据id修改
            LambdaQueryWrapper<ServiceAreaOperators> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(null != operators.getId(),
                    ServiceAreaOperators::getId,
                    operators.getId());

            return operatorsMapper.update(operators,lambdaQueryWrapper);
        }
    }




    /**
     * 删除数据
     * @param operators
     * @return
     * @throws Exception
     */
    @Override
    public int deleteOperators(List<ServiceAreaOperators> operators)throws Exception {

        //如果没有数据或数据为空，直接返回0
        if(null == operators || operators.size() == 0){
            return 0;
        }

        //封装要删除的id集合
        List<Integer> deleteBatchIds = new ArrayList<>();

        //把要删除的id放在deleteBatchIds集合中
        for (ServiceAreaOperators operator : operators) {
            deleteBatchIds.add(operator.getId());
        }

        return operatorsMapper.deleteBatchIds(deleteBatchIds);


    }


    @Override
    public Page<ServiceAreaOperators> getCollectionData(Map<String, Object> params) {
        return null;
    }

    /**
     * 前台登录
     * @param params
     * @return
     */
    @Override
    public ServiceAreaOperators login(Map<String, Object> params) {
        //先根据工号查询用户信息
        List<ServiceAreaOperators> operationByJobNumber = operatorsMapper.getOperationByJobNumber((String) params.get("jobNumber"));

        //如果根据工号找到，再匹配密码是否正确
        if(null != operationByJobNumber && operationByJobNumber.size() > 0){
            String decryptPassWord = PassWordUtils.decrypt(operationByJobNumber.get(0).getPassWord());
            if(decryptPassWord.equals(params.get("passWord"))){
                return operationByJobNumber.get(0);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    /**
     * 前台登录
     * @param params
     * @return
     */
    @Override
    public int updatePassword(Map<String, Object> params) {
        //先根据工号查询用户信息
        List<ServiceAreaOperators> operationByJobNumber = operatorsMapper.getOperationByJobNumber((String) params.get("jobNumber"));
        //如果根据工号找到，再匹配密码是否正确
        if(null != operationByJobNumber && operationByJobNumber.size() > 0){
            ServiceAreaOperators serviceAreaOper=operationByJobNumber.get(0);
            //String decryptPassWord = PassWordUtils.decrypt(operationByJobNumber.get(0).getPassWord());
            String enPassWord = PassWordUtils.encrypt((String)params.get("password"));
            serviceAreaOper.setPassWord(enPassWord);
            //设置修改条件，根据id修改
            LambdaQueryWrapper<ServiceAreaOperators> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(null != serviceAreaOper.getId(),
                    ServiceAreaOperators::getId,
                    serviceAreaOper.getId());

            return operatorsMapper.update(serviceAreaOper,lambdaQueryWrapper);


        }else{
            return -1;
        }
    }

}
