package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.OrderMapper;
import com.lightinit.hsdataplatform.mapper.RefundMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.RefundSearchModel;
import com.lightinit.hsdataplatform.service.admin.IRefundService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RefundServiceImpl implements IRefundService {
    @Autowired
    private RefundMapper refundMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<Refund> queryList(RefundSearchModel refundSearchModel, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RefundExample example=new RefundExample();
        RefundExample.Criteria criteria=example.createCriteria();
        if(refundSearchModel!=null) {
            if (StringUtils.isNotEmpty(refundSearchModel.getOrderNumber())){
                criteria.andOrderNumberLike("%" + refundSearchModel.getOrderNumber() + "%");
            }
        }

        example.setPage(pager);

        return refundMapper.selectByExample(example);
    }

    @Override
    public long queryCount(RefundSearchModel refundSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        RefundExample example=new RefundExample();
        RefundExample.Criteria criteria=example.createCriteria();
        if(refundSearchModel!=null) {
            if (StringUtils.isNotEmpty(refundSearchModel.getOrderNumber())){
                criteria.andOrderNumberLike("%" + refundSearchModel.getOrderNumber() + "%");
            }
        }

        return refundMapper.countByExample(example);
    }

    @Override
    @Transactional
    public ResultState add(Refund inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        int  sqlResult =0; int sqlResult1 =0;
            if(inputModel.getOrderNumber()!=null && !inputModel.getOrderNumber().equals("")) {

                sqlResult1 =  orderMapper.updateOrder(inputModel.getOrderNumber());
                if (!( sqlResult1>0)){
                    resultState.setStateCode(ResultStateCode.INVALID_DATA);
                    resultState.setMsg("订单号不存在");
                    return resultState;
                }
            }

        sqlResult = refundMapper.insert(inputModel);

        if (!(sqlResult > 0 )){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;

    }

    @Override
    public ResultState<Long> delete(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        RefundExample example=new RefundExample();
        RefundExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        if (!(refundMapper.deleteByExample(example)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }

    @Override
    public Object queryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return refundMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public ResultState edit(Refund inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        if (inputModel.getRefundAmount() == null){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("输入金额不合法或者为空");
            return resultState;
        }
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        RefundExample example=new RefundExample();
        RefundExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(inputModel.getId());
        int  sqlResult =0;int sqlResult1 =0;
        if(inputModel.getOrderNumber()!=null && !inputModel.getOrderNumber().equals("")) {

            sqlResult1 =  orderMapper.updateOrder(inputModel.getOrderNumber());
            if (!( sqlResult1>0)){
                resultState.setStateCode(ResultStateCode.INVALID_DATA);
                resultState.setMsg("订单号不存在");
                return resultState;
            }
        }
        sqlResult = refundMapper.updateByExampleSelective(inputModel,example);

        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据编辑失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Override
    public Object detail(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return refundMapper.selectByPrimaryKey(id);
    }
}
