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.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.model.MtTableCategory;
import com.fuint.common.service.TableCategoryService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtTableCategoryMapper;
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 TableCategoryServiceImpl extends ServiceImpl<MtTableCategoryMapper, MtTableCategory> implements TableCategoryService {

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

    private MtTableCategoryMapper mtTableCategoryMapper;

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

        lambdaQueryWrapper.orderByAsc(MtTableCategory::getId);
        List<MtTableCategory> dataList = mtTableCategoryMapper.selectList(lambdaQueryWrapper);

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

        return paginationResponse;
    }

    /**
     * 添加餐桌分类表
     *
     * @param mtTableCategory 餐桌分类表信息
     * @return
     */
    @Override
    @OperationServiceLog(description = "新增餐桌分类表")
    public MtTableCategory addTableCategory(MtTableCategory mtTableCategory) throws BusinessCheckException {
        mtTableCategory.setDeleteFlag(0);
        mtTableCategory.setUpdateTime(new Date());
        mtTableCategory.setCreateTime(new Date());
        int id = mtTableCategoryMapper.insert(mtTableCategory);
        if (id > 0) {
            return mtTableCategory;
        } else {
            throw new BusinessCheckException("新增餐桌分类表数据失败");
        }
    }

    /**
     * 根据ID获餐桌分类表取息
     *
     * @param id 餐桌分类表ID
     * @return
     */
    @Override
    public MtTableCategory queryTableCategoryById(Long id) {
        return mtTableCategoryMapper.selectById(id);
    }

    /**
     * 根据ID删除餐桌分类表
     *
     * @param id 餐桌分类表ID
     * @param operator 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "删除餐桌分类表")
    public void deleteTableCategory(Long id, String operator) {
        MtTableCategory mtTableCategory = queryTableCategoryById(id);
        if (null == mtTableCategory) {
            return;
        }
        mtTableCategory.setDeleteFlag(1);
        mtTableCategory.setUpdateTime(new Date());
        mtTableCategoryMapper.updateById(mtTableCategory);
    }

    /**
     * 修改餐桌分类表数据
     *
     * @param mtTableCategory
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "更新餐桌分类表")
    public MtTableCategory updateTableCategory(MtTableCategory mtTableCategory) throws BusinessCheckException {
        mtTableCategory = queryTableCategoryById(mtTableCategory.getId());
        if (mtTableCategory == null) {
            throw new BusinessCheckException("该餐桌分类表状态异常");
        }
        mtTableCategory.setUpdateTime(new Date());
        mtTableCategoryMapper.updateById(mtTableCategory);
        return mtTableCategory;
    }

   /**
    * 根据条件搜索餐桌分类表
    *
    * @param  params 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public List<MtTableCategory> queryTableCategoryListByParams(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<MtTableCategory> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(storeId)) {
            lambdaQueryWrapper.and(wq -> wq
                    .eq(MtTableCategory::getStoreId, 0)
                    .or()
                    .eq(MtTableCategory::getStoreId, storeId));
        }

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