package com.fzm.mall.server.admin.opration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.opration.entity.dto.FunctionDTO;
import com.fzm.mall.server.admin.opration.entity.dto.FunctionSaveDTO;
import com.fzm.mall.server.admin.opration.entity.dto.FunctionUpdateDTO;
import com.fzm.mall.server.admin.opration.mapper.FunctionMapper;
import com.fzm.mall.server.admin.opration.model.Function;
import com.fzm.mall.server.admin.opration.service.IFunctionService;
import com.fzm.mall.server.admin.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 运营中心-功能区 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-22
 */
@Service
@Transactional
public class FunctionServiceImpl extends ServiceImpl<FunctionMapper, Function> implements IFunctionService {

    @Resource
    private FunctionMapper functionMapper;

    @Override
    public void save(FunctionSaveDTO functionSaveDTO) {
        Function function = CopyUtil.to(functionSaveDTO, Function.class);
        function.init();

        functionMapper.insert(function);
    }

    @Override
    public void update(FunctionUpdateDTO functionUpdateDTO) {
        Function function = CopyUtil.to(functionUpdateDTO, Function.class);

        UpdateWrapper<Function> wrapper = new UpdateWrapper<>();
        wrapper.eq("operation_function_id", function.getOperationFunctionId())
                .eq("merchant_id", function.getMerchantId())
                .eq("update_time", function.getUpdateTime());
        int update = functionMapper.update(function, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    /**
     * 逻辑删除
     *
     * @param operationFunctionId
     * @param merchantId
     */
    @Override
    public void delete(String operationFunctionId, String merchantId) {
        UpdateWrapper<Function> wrapper = new UpdateWrapper<>();
        wrapper.eq("operation_function_id", operationFunctionId)
                .eq("merchant_id", merchantId)
                .set("status", true);
        functionMapper.update(null, wrapper);
    }

    @Override
    public FunctionDTO read(String operationFunctionId, String merchantId) {
        QueryWrapper<Function> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operation_function_id", operationFunctionId)
                .eq("merchant_id", merchantId);
        Function function = functionMapper.selectOne(queryWrapper);

        return CopyUtil.to(function, FunctionDTO.class);
    }

    @Override
    public List<FunctionDTO> list(String merchantId) {
        QueryWrapper<Function> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", merchantId)
                .eq("status", false)
                .orderByAsc("display_order")
                .orderByDesc("update_time");
        List<Function> result = functionMapper.selectList(queryWrapper);
        return CopyUtil.toList(result, FunctionDTO.class);
    }
}
