package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.mapper.mst.MenuFrequencyMapper;
import com.dk.mdm.model.pojo.mst.ComMenu;
import com.dk.mdm.mapper.mst.ComMenuMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.MenuFrequency;
import com.dk.mdm.model.query.mst.ComMenuQuery;
import com.dk.mdm.model.query.mst.MenuFrequencyQuery;
import com.dk.mdm.model.query.sale.OrderQuery;
import com.dk.mdm.model.response.mst.ComMenuResponse;
import com.dk.mdm.model.response.mst.MenuFrequencyResponse;
import com.dk.mdm.model.response.sale.OrderResponse;
import com.dk.mdm.model.vo.mst.ComMenuVO;
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;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ComMenuService extends BaseService<ComMenu> {

    @Override
    public BaseMapper<ComMenu> getRepository() {
        return comMenuMapper;
    }

    @Autowired
    private ComMenuMapper comMenuMapper;

    @Autowired
    private MenuFrequencyMapper menuFrequencyMapper;

    @Autowired
    private AuthUtils authUtils;

    /**
     * @desc : 条件查询
     * @author : 周兴
     * @date : 2024/3/29 10:40
     */
    public ResponseResultVO<?> selectMenu(ComMenuQuery comMenuQuery) {
        // 先查常用功能
        List<ComMenuResponse> comMenuList = comMenuMapper.selectByCond(comMenuQuery);
        Integer max = 9;
        if (comMenuList.size() < max) {
            List<String> exceptmenu = comMenuList.stream().map(it -> it.getMenuUuid()).collect(Collectors.toList());
            // 不足的用频率功能补
            List<MenuFrequencyResponse>  MenuFrequencyList = menuFrequencyMapper.selectByCond(new MenuFrequencyQuery()
                    .setStaffId(comMenuQuery.getStaffId())
                    .setAppCode(comMenuQuery.getAppCode())
                    .setGradeCode(comMenuQuery.getGradeCode())
                    .setQueryLimit(max - comMenuList.size())
                    .setExceptMenu(exceptmenu))   ;
            MenuFrequencyList.forEach(it->{
                ComMenuResponse res = new ComMenuResponse().setMenuUuid(it.getMenuUuid())
                        .setMenuName(it.getMenuName()).setStaffId(it.getStaffId())
                        .setObjectCode(it.getObjectCode()).setObjectIcon(it.getObjectIcon())
                        .setObjectPath(it.getObjectPath());
                comMenuList.add(res);
            });
        }
        return ResponseResultUtil.success(comMenuList);
    }

    /**
     * @desc : 查询常用功能，不够的功能频率表去补
     * @author : 周兴
     * @date : 2024/3/29 10:40
     */
    public ResponseResultVO<?> selectByCond(ComMenuQuery comMenuQuery) {
        return ResponseResultUtil.success(comMenuMapper.selectByCond(comMenuQuery));
    }

    /**
     * @desc : 保存常用功能频率表
     * @author : 周兴
     * @date : 2024/3/29 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> save(List<ComMenu> comMenuList) {
        // 先删除该员工的常用功能
        String staffId = authUtils.getStaff().getStaffId();
        comMenuMapper.deleteMenuByStaff(staffId);
        // 保存常用功能
        comMenuMapper.insertBatch(comMenuList);
        return ResponseResultUtil.success();
    }
}
