package com.jwipc.shopping.controller.pc;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.base.Strings;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.shopping.constant.ModuleConst;
import com.jwipc.shopping.entity.ShoppingInvoice;
import com.jwipc.shopping.logic.OnlineMallLogic;
import com.jwipc.shopping.model.query.ShoppingQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/14 0014 20:08
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/invoice/rest")
@RequiredArgsConstructor
@Slf4j
public class ShoppingInvoiceRestController {


    @Autowired
    OnlineMallLogic onlineMallLogic;


    /**
     * 列表数据查询
     *
     * @param page 当前页码
     * @param rows 每页显示行数
     * @param query 查询条件
     * @return 结果
     */
    @RequestMapping("/list")
    public PageView list(ShoppingQuery query) {
        return onlineMallLogic.listInvoicePage(query);
    }


    /**
     * 新建发票
     **/
    @RequestMapping("data/save")
    public ResponseEntity addInvoice(@RequestBody ShoppingInvoice data) {
        if (ObjectUtil.isNull(data)) {
            return ResponseEntity.badRequest().body("参数错误, 请检查");
        }
        if (data.getId()!=null&&data.getId()!=0L) {
            data.setUpdateUserId(LoginUtil.getUserId());
            data.setUpdateUserCode(LoginUtil.getAccount());
            data.setUpdateUserName(LoginUtil.getName());
            data.setUpdateTime(LocalDateTime.now());
        } else {
            data.setCreateUserId(LoginUtil.getUserId());
            data.setCreateUserCode(LoginUtil.getAccount());
            data.setCreateUserName(LoginUtil.getName());
            data.setCreateTime(LocalDateTime.now());
            data.setId(null);
        }
        onlineMallLogic.addOrEditInvoice(data);
        return ResponseEntity.ok(true);
    }


    /**
     * 数据删除
     *
     * @param invoiceId 待删除的关系数据ID
     * @return 结果
     */
    @AuditLogInter(value = "删除数据", module = "商城发票信息")
    @RequestMapping("/del")
    public ResponseEntity<?> dataDel(String invoiceId) {
        if (Strings.isNullOrEmpty(invoiceId)) {
            return ResponseEntity.badRequest().body("参数错误, 请检查");
        }
        ShoppingInvoice invoice = new ShoppingInvoice();
        invoice.setId(Long.valueOf(invoiceId));
        invoice.setDeleteFlag(1);
        invoice.setUpdateTime(LocalDateTime.now());
        invoice.setUpdateUserId(LoginUtil.getUserId());
        invoice.setUpdateUserCode(LoginUtil.getAccount());
        invoice.setUpdateUserName(LoginUtil.getName());
        onlineMallLogic.addOrEditInvoice(invoice);
        return ResponseEntity.ok(true);
    }

}
