package com.zcloud.modules.custormerInfo.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.DateUtil;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.custormerInfo.entity.BusInvoiceManagementEntity;
import com.zcloud.modules.custormerInfo.service.BusInvoiceManagementService;
import com.zcloud.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author lizenan
 * @Desc 发票抬头管理表(BusInvoiceManagement)表控制层
 * @Date 2024-04-03 15:34:41
 */
@Api(tags = "发票抬头管理表")
@RestController
@RequestMapping("busInvoiceManagement")
public class BusInvoiceManagementController extends AbstractController {

    @Autowired
    private BusInvoiceManagementService busInvoiceManagementService;

    @ApiOperation("发票抬头管理表-新增数据")
    @SysLog("发票抬头管理表-新增数据")
    @PostMapping("/save")
    public R insert(@RequestBody BusInvoiceManagementEntity param) {
        param.setCreator(getUserId());
		param.setOperator(getUserId());
        if ("10".equals(param.getInvoiceType())) { //个人抬头 只保留发票抬头
            param.setAccountBank(null);
            param.setBankAccount(null);
            param.setRegisterAddress(null);
            param.setRegisterPhone(null);
            param.setTaxNum(null);
        }
        busInvoiceManagementService.save(param);

        return R.ok();
    }

    @ApiOperation("发票抬头管理表-编辑数据")
    @SysLog("发票抬头管理表-编辑数据")
    @PostMapping("/update")
    public R update(@RequestBody BusInvoiceManagementEntity param) {
        param.setOperator(getUserId());
        if ("10".equals(param.getInvoiceType())) { //个人抬头 只保留发票抬头
            param.setAccountBank(null);
            param.setBankAccount(null);
            param.setRegisterAddress(null);
            param.setRegisterPhone(null);
            param.setTaxNum(null);
        }
        busInvoiceManagementService.updateById(param);

        return R.ok();
    }


    @ApiOperation("发票抬头管理表-删除数据")
    @SysLog("发票抬头管理表-删除数据")
    @PostMapping("/delete")
    public R delete(@RequestBody BusInvoiceManagementEntity param) {
        busInvoiceManagementService.delete(param.getInvoiceId(),getUserId());
        return R.ok();
    }

    /**
     * 批量删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/deleteAll")
    @SysLog("发票抬头管理表-批量删除数据")
    public Object deleteAll(@RequestBody Map<String,String> params) throws Exception {
        List<BusInvoiceManagementEntity> busInvoiceManagementEntities = new ArrayList<>();
        String username = this.getUsername();
        String date = DateUtil.date2Str(new Date());
        String invoiceIds = params.get("invoiceIds");
        for (String invoiceId: invoiceIds.split(",")) {
            BusInvoiceManagementEntity busInvoiceManagementEntity = new BusInvoiceManagementEntity();
            busInvoiceManagementEntity.setInvoiceId(invoiceId);
            busInvoiceManagementEntity.setOperatTime(date);
            busInvoiceManagementEntity.setOperator(username);
            busInvoiceManagementEntity.setIsDelete(1);
            busInvoiceManagementEntities.add(busInvoiceManagementEntity);
        }
        busInvoiceManagementService.saveOrUpdateBatch(busInvoiceManagementEntities);
        return R.ok();

    }


    @ApiOperation("发票抬头管理表-分页查询")
    @SysLog("发票抬头管理表-分页查询")
    @PostMapping("/listPage")
    public R listPage(@RequestBody Map<String, Object> params) {
        PageUtils page = busInvoiceManagementService.listPage(params);

		return R.ok().put("page", page);
    }


    @ApiOperation("发票抬头管理表-根据CustomerinfoId查询")
    @SysLog("发票抬头管理表-根据CustomerinfoId查询")
    @PostMapping("/listByCustomerinfoId")
    public R listByCustomerinfoId(@RequestBody BusInvoiceManagementEntity param) {
        List<BusInvoiceManagementEntity> list = busInvoiceManagementService.listByCustomerinfoId(param);
        return R.ok().put("data", list);
    }


    @ApiOperation("发票抬头管理表-根据ID查详情")
    @SysLog("发票抬头管理表-根据ID查详情")
    @PostMapping("/info")
    public R info(@RequestBody BusInvoiceManagementEntity param) {
        BusInvoiceManagementEntity busInvoiceManagement = busInvoiceManagementService.info(param.getInvoiceId());
        return R.ok().put("invoice", busInvoiceManagement);
    }

}
