package com.muyu.enterprise.controller;

import cn.hutool.core.bean.BeanUtil;
import com.muyu.common.core.domain.Result;
import com.muyu.enterprise.controller.form.*;
import com.muyu.enterprise.domain.Enterprise;
import com.muyu.enterprise.service.EnterpriseService;
import com.muyu.enterprise.util.PageUtils;
import com.muyu.enterprise.util.R;
import jakarta.validation.Valid;
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
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 查询分页企业信息
     * @param form
     * @return
     */
    @PostMapping("/selectEnterprise")
    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")
    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")
    public Result searchById(@RequestParam("enterpriseId") Integer enterpriseId){
        HashMap map = enterpriseService.searchById(enterpriseId);
        return Result.success(map);
    }


    /**
     * 修改企业信息
     * @param form
     * @return
     */
    @PostMapping("/updateEnterEnterprise")
    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")
    public Result deleteByIds(@RequestBody @Valid DeleteEnterpriseByIds form){
        int rows = enterpriseService.deleteByIds(form.getIds());
        return Result.success(rows);
    }


}
