package com.muyu.server.controller;

import cn.hutool.core.bean.BeanUtil;
import com.muyu.common.core.domain.Result;
import com.muyu.common.domain.Enterprise;
import com.muyu.common.util.PageUtils;
import com.muyu.server.controller.form.DeleteEnterpriseByIds;
import com.muyu.server.controller.form.InsertEnterprise;
import com.muyu.server.controller.form.SearchEnterpriseName;
import com.muyu.server.controller.form.UpdateEnterprise;
import com.muyu.server.service.EnterpriseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 企业运营控制层
 * @author yuping
 * @Description 企业运营控制层
 * @Version 1.0
 * @Data 2024-09-26 20:24:05
 */
@RestController
@RequestMapping("/enterprise")
@Log4j2
@Tag(name = "企业运营管理",description = "企业运营管理")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 查询分页企业信息
     * @param form
     * @return
     */
    @PostMapping("/selectEnterprise")
    @Operation(summary = "查询分页企业信息",description = "查询分页企业信息")
    public Result selectEnterprise(@RequestBody @Valid SearchEnterpriseName form) {
        Integer page = form.getPage();
        Integer length = form.getLength();
        int start = (page - 1) * length;
        Map param = BeanUtil.beanToMap(form);
        param.put("start", start);
        PageUtils pageUtils = enterpriseService.selectEnterprise(param);

        System.out.println(pageUtils);

        return Result.success(pageUtils);
    }


    /**
     * 新增企业信息
     * @param form
     * @return
     */
    @PostMapping("/insert")
    @Operation(summary = "新增企业信息",description = "新增企业信息")
    public Result insert(@RequestBody @Valid InsertEnterprise form){
        Enterprise enterprise = new Enterprise();

        enterprise.setEnterpriseName(form.getEnterpriseName());
        enterprise.setEnterpriseCarCount(form.getEnterpriseCarCount());
        enterprise.setEnterpriseFenceCount(form.getEnterpriseFenceCount());

        int rows = enterpriseService.insert(enterprise);
        return Result.success(rows);
    }


    /**
     * 根据编号查询企业信息
     * @param enterpriseId
     * @return
     */
    @GetMapping("/searchById")
    @Operation(summary = "根据编号查询企业信息",description = "根据编号查询企业信息")
    public Result searchById(@RequestParam("enterpriseId") Integer enterpriseId){
        HashMap map = enterpriseService.searchById(enterpriseId);
        return Result.success(map);
    }


    /**
     * 修改企业信息
     * @param form
     * @return
     */
    @PostMapping("/updateEnterEnterprise")
    @Operation(summary = "修改企业信息",description = "修改企业信息")
    public Result updateEnterprise(@RequestBody @Valid UpdateEnterprise form){
        Enterprise enterprise = new Enterprise();

        enterprise.setEnterpriseId(form.getEnterpriseId());
        enterprise.setEnterpriseName(form.getEnterpriseName());
        enterprise.setEnterpriseCarCount(form.getEnterpriseCarCount());
        enterprise.setEnterpriseFenceCount(form.getEnterpriseFenceCount());

        int rows = enterpriseService.updateEnterprise(enterprise);
        return Result.success(rows);
    }


    /**
     * 删除企业信息
     * @param form
     * @return
     */
    @PostMapping("/deleteByIds")
    @Operation(summary = "删除企业信息",description = "删除企业信息")
    public Result deleteByIds(@RequestBody @Valid DeleteEnterpriseByIds form){
        int rows = enterpriseService.deleteByIds(form.getIds());
        return Result.success(rows);
    }


}
