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

import cn.com.ty.lift.ud.userInvoice.controller.query.UserInvoiceQuery;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.extension.service.IService;

import cn.com.ty.lift.ud.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户发票抬头 API
 * 
 * @author Xson
 * @date 2020-05-21
 */
@Api(tags = "用户发票抬头 API")
@RestController
@RequestMapping("/api/userInvoices")
public class UserInvoiceController extends BaseController<UserInvoice, UserInvoiceQuery> {

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

    private @Autowired IUserInvoiceService userInvoiceService;

    @Override
    protected IService<UserInvoice> getService() {
        return userInvoiceService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<UserInvoice> create(@RequestBody UserInvoice entity) {
        if (entity.getIsDefault() == 1) {
            userInvoiceService.updateIsDefaultByUserId(entity.getUserId());
        }
        boolean saveFlag = getService().save(entity);
        // E byId = getService().getById(entity.getId());

        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    // 修改
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public RestResponse update(@RequestBody UserInvoice request) {
        if (request.getIsDefault() == 1) {
            userInvoiceService.updateIsDefaultByUserId(request.getUserId());
        }
        boolean updateFlag = getService().updateById(request);
        // E e = getService().getById(request.getId());

        if (updateFlag)
            return RestResponse.success(null, "Updated successfully");
        else
            return RestResponse.fail("Updated unsuccessfully");
    }
}