package com.dk.mdm.service.mst;

import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.MenuFrequencyConvert;
import com.dk.mdm.model.pojo.mst.MenuFrequency;
import com.dk.mdm.mapper.mst.MenuFrequencyMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.response.mst.MenuFrequencyResponse;
import com.dk.mdm.model.vo.mst.MenuFrequencyVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MenuFrequencyService extends BaseService<MenuFrequency> {

    @Override
    public BaseMapper<MenuFrequency> getRepository() {
        return menuFrequencyMapper;
    }

    @Autowired
    private MenuFrequencyMapper menuFrequencyMapper;

    @Autowired
    private MenuFrequencyConvert menuFrequencyConvert;

    /**
     * @desc : 保存用户功能频率表
     * @author : 周兴
     * @date : 2024/3/28 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> save(MenuFrequencyVO menuFrequencyVO) {
        MenuFrequency menuFrequency = menuFrequencyConvert.convertToPo(menuFrequencyVO);
        // 先查出数量
        MenuFrequencyResponse mf = menuFrequencyMapper.selectMenuById(menuFrequency);
        if (mf == null) {
            menuFrequency.setQty(1);
        }else{
            menuFrequency.setQty(mf.getQty() + 1);
        }
        // 保存用户功能频率表
        menuFrequencyMapper.save(menuFrequency);
        return ResponseResultUtil.success();
    }

}
