package com.sqx.modules.printInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.printInfo.dao.PrintInfoDao;
import com.sqx.modules.printInfo.entity.PrintInfo;
import com.sqx.modules.printInfo.service.PrintInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2022-11-04
 */
@Service
public class PrintInfoServiceImpl extends ServiceImpl<PrintInfoDao, PrintInfo> implements PrintInfoService {

    @Override
    public Result selectPrintInfoList(Integer page, Integer limit) {
        Page<PrintInfo> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.selectPage(pages,null));
        return Result.success().put("data", pageUtils);
    }

    @Transactional
    @Override
    public Result insertPrintInfo(PrintInfo PrintInfo) {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        PrintInfo.setCreateTime(format);
        PrintInfo.setUpdateTime(format);
        PrintInfo.setDeleteFlag(0);
        baseMapper.insert(PrintInfo);
        return Result.success();
    }

    @Transactional
    @Override
    public Result updatePrintInfo(PrintInfo PrintInfo) {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        PrintInfo.setUpdateTime(format);
        baseMapper.updateById(PrintInfo);
        return Result.success();
    }

    @Override
    public Result selectPrintInfoById(Integer id) {
        PrintInfo PrintInfo = baseMapper.selectById(id);
        return Result.success().put("data", PrintInfo);
    }

    @Override
    public Result deletePrintInfo(Integer id) {
        baseMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public List<PrintInfo> printInfos() {
        QueryWrapper<PrintInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        List<PrintInfo> result = baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public int existInfo(Integer type,String field) {
        QueryWrapper<PrintInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",type);
        queryWrapper.eq("print_field",field);
        queryWrapper.eq("delete_flag",0);
        int result = baseMapper.selectCount(queryWrapper);
        return result;
    }

    @Override
    public PrintInfo existInfoData(Integer type, String field) {
        QueryWrapper<PrintInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",type);
        queryWrapper.eq("print_field",field);
        queryWrapper.eq("delete_flag",0);
        PrintInfo result = baseMapper.selectOne(queryWrapper);
        return result;
    }
}
