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

import java.math.BigDecimal;
import java.util.List;

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.chat.mapper.LiftCaseMapper;
import cn.com.ty.lift.ud.chat.mapper.entity.LiftCaseEntity;
import cn.com.ty.lift.ud.operation.mapper.OtherSettingMapper;
import cn.com.ty.lift.ud.operation.mapper.entity.OtherSettingEntity;
import cn.com.ty.lift.ud.payment.mapper.ApplyInvoiceImgMapper;
import cn.com.ty.lift.ud.payment.mapper.ApplyInvoiceItemMapper;
import cn.com.ty.lift.ud.payment.mapper.ApplyInvoiceMapper;
import cn.com.ty.lift.ud.payment.mapper.entity.ApplyInvoiceEntity;
import cn.com.ty.lift.ud.payment.mapper.entity.ApplyInvoiceImgEntity;
import cn.com.ty.lift.ud.payment.mapper.entity.ApplyInvoiceItemEntity;
import cn.com.ty.lift.ud.payment.service.IApplyInvoiceService;
import cn.com.ty.lift.ud.userInvoice.mapper.UserInvoiceMapper;
import cn.com.ty.lift.ud.userInvoice.mapper.entity.UserInvoice;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 申请发票 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class ApplyInvoiceServiceImpl extends ServiceImpl<ApplyInvoiceMapper, ApplyInvoiceEntity>
    implements IApplyInvoiceService {

    private @Autowired ApplyInvoiceMapper applyInvoiceMapper;
    private @Autowired ApplyInvoiceImgMapper applyInvoiceImgMapper;
    private @Autowired ApplyInvoiceItemMapper applyInvoiceItemMapper;
    private @Autowired UserInvoiceMapper userInvoiceMapper;
    private @Autowired LiftCaseMapper caseMapper;
    @Autowired
    private OtherSettingMapper otherSettingMapper;

    @Override
    public RestResponse finOneById(Long id) {
        ApplyInvoiceEntity invoice = applyInvoiceMapper.selectById(id);
        if (invoice != null) {
            List<ApplyInvoiceImgEntity> findByInvoiceId = applyInvoiceImgMapper.findByInvoiceId(invoice.getId());
            List<ApplyInvoiceItemEntity> findByInvoiceId2 = applyInvoiceItemMapper.findByInvoiceId(invoice.getId());
            UserInvoice selectById = userInvoiceMapper.selectById(invoice.getUserInvoiceId());
            invoice.setInvoiceImg(findByInvoiceId);
            invoice.setInvoiceItem(findByInvoiceId2);
            invoice.setUserInvoice(selectById);
        }
        UserInvoice selectById = userInvoiceMapper.selectById(invoice.getUserInvoiceId());
        invoice.setUserInvoice(selectById);
        return RestResponse.success(invoice, "Queried successfully");
    }

    @Override
    public void applyInvoice(ApplyInvoiceEntity applyInvoiceEntity) {
        applyInvoiceEntity.setCheckFlag(2);
        BigDecimal counts = BigDecimal.ZERO;
        applyInvoiceMapper.insert(applyInvoiceEntity);
        OtherSettingEntity latestVersion = otherSettingMapper.getLatestVersion();
        for (ApplyInvoiceItemEntity item : applyInvoiceEntity.getInvoiceItem()) {
            LiftCaseEntity selectById = caseMapper.selectById(item.getId());
            selectById.setIsInvoice(1);
            caseMapper.updateById(selectById);
            item.setDataId(selectById.getId());
            item.setInvoiceId(applyInvoiceEntity.getId());
            item.setDw("次");
            item.setXmmc("咨询服务");
            item.setGgxh("无");
            item.setFphxz(1);
            item.setXmdj(selectById.getPayCost());
            item.setXmze(selectById.getPayCost());
            item.setXmsl(1);
            item.setSl(latestVersion.getDefaultTax());
            item.setSe((new BigDecimal(item.getSl())).multiply(item.getXmze()).divide(new BigDecimal("100")));
            applyInvoiceItemMapper.insert(item);
            counts = counts.add(item.getSe().add(item.getXmze()));
        }
        applyInvoiceMapper.updateJshjById( applyInvoiceEntity.getId(),counts);
    }

    @Override
    public RestResponse examine(ApplyInvoiceEntity neDto) {
        if (neDto.getCheckFlag() == 1) {
            applyInvoiceMapper.examineById(neDto.getId());
            // 調工商局税务接口

            return RestResponse.success(null, "审批通过");
        } else {
            // 修改状态为 驳回，并加上驳回理由
            applyInvoiceMapper.updateInfoById(neDto.getId(), neDto.getReason());
            // todo 发送消息给该用户
            return RestResponse.fail(null, "审批驳回");
        }
    }

}
