package com.repair.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.common.utils.AesUtils;
import com.repair.mapper.RepairAdminMapper;
import com.repair.model.pojo.RepairAdmin;
import com.repair.model.request.updateRepairAdminRequest;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-05
 */
@Service
public class RepairAdminServiceImpl extends ServiceImpl<RepairAdminMapper, RepairAdmin> implements RepairAdminService {

    @Autowired
    private RepairAdminMapper repairAdminMapper;

    @Override
    public int insertRepairAdmin(RepairAdmin ra) {
        ra.setIsSuper(0);
        ra.setStatu(0);
        ra.setPassword(AesUtils.encrypt(ra.getPassword()));//加密密码
        int result = repairAdminMapper.insert(ra);
        return result;
    }

    @Override
    public int deleteRepairAdminById(String id) {
        int result = repairAdminMapper.deleteById(id);
        return result;
    }

    @Override
    public RepairAdmin getRepairByAccount(String Account) {
        QueryWrapper<RepairAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", Account);
        RepairAdmin result = repairAdminMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public RepairAdmin getRepairByCardnumber(String Cardnumber) {
        QueryWrapper<RepairAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_number", Cardnumber);
        RepairAdmin result = repairAdminMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public RepairAdmin getRepairById(String id) {
        RepairAdmin result = repairAdminMapper.selectById(id);
        return result;
    }

    @Override
    public int updateRepaiRadmin(updateRepairAdminRequest urar) {
        //只更改有值的字段
        RepairAdmin ra = new RepairAdmin();
        ra.setId(urar.getId());
        //超级管理员用户才能更改密码
        if(urar.getPassword() != null){
            urar.setPassword(AesUtils.encrypt(urar.getPassword()));//加密密码
        }
        ra.setUsername(urar.getUsername());
        ra.setPhone(urar.getPhone());
        int result = repairAdminMapper.updateById(ra);
        return result;
    }

    @Override
    public int updateRepairAdmin(RepairAdmin ra) {
        int result = repairAdminMapper.updateById(ra);
        return result;
    }

    @Override
    public List<RepairAdmin> queryRepairAdminList(String account, String startTime, String endTime) {
        QueryWrapper<RepairAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(account), "account", account);
        queryWrapper.between(StringUtils.hasText(startTime) && StringUtils.hasText(endTime),"update_time", startTime, endTime);
        queryWrapper.eq("is_mobile",0);

        List<RepairAdmin> result = repairAdminMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<RepairAdmin> queryPageList(int currentPage, int pageCount, String account, String startTime, String endTime) {
        Page<RepairAdmin> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(account), "account", account);
        queryWrapper.between(StringUtils.hasText(startTime) && StringUtils.hasText(endTime),"update_time", startTime, endTime);
        queryWrapper.eq("is_mobile",0);
        IPage<RepairAdmin> result = repairAdminMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }
}
