package cn.com.ty.lift.ud.payment.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.chat.mapper.entity.LiftCaseEntity;
import cn.com.ty.lift.ud.chat.service.ILiftCaseService;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.payment.controller.query.ApplyInvoiceQuery;
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.IApplyInvoiceImgService;
import cn.com.ty.lift.ud.payment.service.IApplyInvoiceItemService;
import cn.com.ty.lift.ud.payment.service.IApplyInvoiceService;
import cn.com.ty.lift.ud.userInvoice.mapper.entity.UserInvoice;
import cn.com.ty.lift.ud.userInvoice.service.IUserInvoiceService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 申请发票 API Jing
 * 
 * @author Xson
 * @date 2020-04-22
 */
@Api(tags = "申请发票 API- 盛文静")
@RestController
@RequestMapping("/api/applyInvoices")
public class ApplyInvoiceController extends BaseController<ApplyInvoiceEntity, ApplyInvoiceQuery> {

    private final Logger log = LoggerFactory.getLogger(ApplyInvoiceController.class);

    private @Autowired IApplyInvoiceService applyInvoiceService;
    private @Autowired IApplyInvoiceItemService iApplyInvoiceItemService;
    private @Autowired IApplyInvoiceImgService iApplyInvoiceImgService;
    private @Autowired IUserInvoiceService userInvoiceService;

    @Override
    protected IService<ApplyInvoiceEntity> getService() {
        return applyInvoiceService;
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable("id") Long id) {
        return applyInvoiceService.finOneById(id);
    }

    // 根据条件查询
    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    public RestResponse getCondition(@RequestBody ApplyInvoiceQuery request) {
        IPage<ApplyInvoiceEntity> listInvoice = getService().page(request.page(), request.condition());
        for (ApplyInvoiceEntity invoice : listInvoice.getRecords()) {
            List<ApplyInvoiceImgEntity> findByInvoiceId = iApplyInvoiceImgService.findByInvoiceId(invoice.getId());
            List<ApplyInvoiceItemEntity> findByInvoiceId2 = iApplyInvoiceItemService.findByInvoiceId(invoice.getId());
            UserInvoice byId = userInvoiceService.getById(invoice.getUserInvoiceId());
            invoice.setInvoiceImg(findByInvoiceId);
            invoice.setInvoiceItem(findByInvoiceId2);
            invoice.setUserInvoice(byId);
        }
        return RestResponse.success(listInvoice, "Queried successfully");
    }

    // 申请发票
    @PostMapping("/applyInvoice")
    @ApiOperation(value = "申请发票")
    public RestResponse applyInvoice(@RequestBody ApplyInvoiceEntity applyInvoiceEntity) {
        applyInvoiceService.applyInvoice(applyInvoiceEntity);
        return RestResponse.success(null, "Queried successfully");
    }

    @ApiOperation(value = "审批 1:通过 0:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody ApplyInvoiceEntity neDto) {
        return applyInvoiceService.examine(neDto);
    }
}