package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.service.TableCategoryService;
import com.fuint.framework.annoation.OperationServiceLog;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.repository.mapper.MtTableCategoryMapper;
import com.fuint.repository.model.MtTableCategory;
import com.fuint.repository.model.MtTableInfo;
import com.fuint.common.service.TableInfoService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtTableInfoMapper;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 餐桌信息服务接口
 *
 * Created by pzg
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class TableInfoServiceImpl extends ServiceImpl<MtTableInfoMapper, MtTableInfo> implements TableInfoService {

    private static final Logger logger = LoggerFactory.getLogger(TableInfoServiceImpl.class);

    private MtTableInfoMapper mtTableInfoMapper;

    private MtTableCategoryMapper tableCategoryMapper;

    /**
     * 分页查询数据列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtTableInfo> queryTableInfoListByPagination(PaginationRequest paginationRequest) {
        Page<MtTableInfo> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtTableInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.ne(MtTableInfo::getDeleteFlag, 1);

        lambdaQueryWrapper.orderByAsc(MtTableInfo::getId);
        List<MtTableInfo> dataList = mtTableInfoMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtTableInfo> paginationResponse = new PaginationResponse(pageImpl, MtTableInfo.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    /**
     * 添加餐桌信息
     *
     * @param mtTableInfo 餐桌信息信息
     * @return
     */
    @Override
    @OperationServiceLog(description = "新增餐桌信息")
    public MtTableInfo addTableInfo(MtTableInfo mtTableInfo) throws BusinessCheckException {
        mtTableInfo.setDeleteFlag(0);
        mtTableInfo.setUpdateTime(new Date());
        mtTableInfo.setCreateTime(new Date());
        Integer id = mtTableInfoMapper.insert(mtTableInfo);
        if (id > 0) {
            return mtTableInfo;
        } else {
            throw new BusinessCheckException("新增餐桌信息数据失败");
        }
    }

    /**
     * 根据ID获餐桌信息取息
     *
     * @param id 餐桌信息ID
     * @return
     */
    @Override
    public MtTableInfo queryTableInfoById(Long id) {
        return mtTableInfoMapper.selectById(id);
    }

    /**
     * 根据ID删除餐桌信息
     *
     * @param id 餐桌信息ID
     * @param operator 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "删除餐桌信息")
    public void deleteTableInfo(Long id, String operator) {
        MtTableInfo mtTableInfo = queryTableInfoById(id);
        if (null == mtTableInfo) {
            return;
        }
        mtTableInfo.setDeleteFlag(1);
        mtTableInfo.setUpdateTime(new Date());
        mtTableInfoMapper.updateById(mtTableInfo);
    }

    /**
     * 修改餐桌信息数据
     *
     * @param mtTableInfo
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "更新餐桌信息")
    public MtTableInfo updateTableInfo(MtTableInfo mtTableInfo) throws BusinessCheckException {
        mtTableInfo = queryTableInfoById(mtTableInfo.getId());
        if (mtTableInfo == null) {
            throw new BusinessCheckException("该餐桌信息状态异常");
        }
        mtTableInfo.setUpdateTime(new Date());
        mtTableInfoMapper.updateById(mtTableInfo);
        return mtTableInfo;
    }

   /**
    * 根据条件搜索餐桌信息
    *
    * @param  params 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public List<MtTableInfo> queryTableInfoListByParams(Map<String, Object> params) {
        String status =  params.get("status") == null ? StatusEnum.ENABLED.getKey(): params.get("status").toString();
        String storeId =  params.get("storeId") == null ? "" : params.get("storeId").toString();
        String merchantId =  params.get("merchantId") == null ? "" : params.get("merchantId").toString();

        LambdaQueryWrapper<MtTableInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();

        lambdaQueryWrapper.orderByAsc(MtTableInfo::getId);
        List<MtTableInfo> dataList = mtTableInfoMapper.selectList(lambdaQueryWrapper);
        return dataList;
    }

    @Override
    public List<MtTableInfo> selectTableListByStoreId(Long storeId, Long categoryId) {
        //通过店铺id获取店铺下的餐桌分类
        MtTableCategory mtTableCategory = tableCategoryMapper.selectOne(new LambdaQueryWrapper<MtTableCategory>()
                .select(MtTableCategory::getId,MtTableCategory::getStoreId)
                .eq(MtTableCategory::getId, categoryId));
        if (Objects.isNull(mtTableCategory) || !storeId.equals(mtTableCategory.getStoreId())) {
            return new ArrayList<>();
        }
        //返回结果
        return this.list(new LambdaQueryWrapper<MtTableInfo>()
                .select(MtTableInfo::getId, MtTableInfo::getTableNumber)
                .eq(MtTableInfo::getCategoryId, categoryId)
                .eq(MtTableInfo::getDeleteFlag, 0));
    }
}
