package cn.com.ty.lift.ud.operation.service.impl;

import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.xwy.boot.web.dto.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.ty.lift.ud.operation.mapper.entity.TaxSettingEntity;

import cn.com.ty.lift.ud.operation.mapper.TaxSettingMapper;
import cn.com.ty.lift.ud.operation.service.ITaxSettingService;

/**
 * 税率设置 -ServiceImpl
 *
 * @author Xson
 * @date 2020-05-04
 */
@Service
@Transactional
public class TaxSettingServiceImpl extends ServiceImpl<TaxSettingMapper,TaxSettingEntity>
    implements ITaxSettingService {

    public static final int EXAMINEFLAG = 1;

    @Autowired
    private TaxSettingMapper taxSettingMapper;

    /**
     * 审批
     * @param neDto
     * @return
     */
    @Override
    public RestResponse examineById(NotExamineDto neDto) {
        if(EXAMINEFLAG == neDto.getExamineFlag()){
            taxSettingMapper.examineById(neDto.getId());
            return RestResponse.success(null, "审批通过");
        }else{
            //修改状态为 驳回，并加上驳回理由
            taxSettingMapper.updateInfoById(neDto.getId(),neDto.getNotExamineReason());
            return RestResponse.fail(null, "审批驳回");
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @Override
    public RestResponse submit(Long id) {
        int submit = taxSettingMapper.submit(id);
        if (submit == 1) {
            return RestResponse.success(null, "提交成功");
        } else {
            return RestResponse.fail(null, "提交失败");
        }
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @Override
    public RestResponse create(TaxSettingEntity entity) {
        Long id = entity.getId();
        if (null == id) {
            //直接点的提交按钮
            entity.setExamineFlag(0);
            int insert = taxSettingMapper.insert(entity);
            if (insert == 1) {
                return RestResponse.success(null, "新建成功");
            } else {
                return RestResponse.fail("新建失败");
            }
        } else {
            //先保存再提交,修改状态为未审核 0
            return submit(id);
        }
    }
}
