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.MtCurrency;
import com.fuint.common.service.CurrencyService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtCurrencyMapper;
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 CurrencyServiceImpl extends ServiceImpl<MtCurrencyMapper, MtCurrency> implements CurrencyService {

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

    private MtCurrencyMapper mtCurrencyMapper;

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

        lambdaQueryWrapper.orderByAsc(MtCurrency::getId);
        List<MtCurrency> dataList = mtCurrencyMapper.selectList(lambdaQueryWrapper);

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

        return paginationResponse;
    }

    /**
     * 添加货币表
     *
     * @param mtCurrency 货币表信息
     * @return
     */
    @Override
    @OperationServiceLog(description = "新增货币表")
    public MtCurrency addCurrency(MtCurrency mtCurrency) throws BusinessCheckException {

        mtCurrency.setUpdateTime(new Date());
        mtCurrency.setCreateTime(new Date());
        Integer id = mtCurrencyMapper.insert(mtCurrency);
        if (id > 0) {
            return mtCurrency;
        } else {
            throw new BusinessCheckException("新增货币表数据失败");
        }
    }

    /**
     * 根据ID获货币表取息
     *
     * @param id 货币表ID
     * @return
     */
    @Override
    public MtCurrency queryCurrencyById(Long id) {
        return mtCurrencyMapper.selectById(id);
    }

    /**
     * 根据ID删除货币表
     *
     * @param id 货币表ID
     * @param operator 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "删除货币表")
    public void deleteCurrency(Long id, String operator) {
        MtCurrency mtCurrency = queryCurrencyById(id);
        if (null == mtCurrency) {
            return;
        }

        mtCurrency.setUpdateTime(new Date());
        mtCurrencyMapper.updateById(mtCurrency);
    }

    /**
     * 修改货币表数据
     *
     * @param mtCurrency
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "更新货币表")
    public MtCurrency updateCurrency(MtCurrency mtCurrency) throws BusinessCheckException {
        mtCurrency = queryCurrencyById(mtCurrency.getId());
        if (mtCurrency == null) {
            throw new BusinessCheckException("该货币表状态异常");
        }
        mtCurrency.setUpdateTime(new Date());
        mtCurrencyMapper.updateById(mtCurrency);
        return mtCurrency;
    }

   /**
    * 根据条件搜索货币表
    *
    * @param  params 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public List<MtCurrency> queryCurrencyListByParams(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<MtCurrency> lambdaQueryWrapper = Wrappers.lambdaQuery();


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