package com.zhgd.xmgl.modules.basicdata.controller.enterprise;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.WorkerType;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IWorkerTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Title: Controller
 * @Description: 工种
 * @author： pengj
 * @date： 2023-03-13
 * @version： V1.0
 */
@RestController
@RequestMapping("/ent/workerType")
@Slf4j
@Api(tags = "工种管理")
public class EntWorkerTypeController {
    @Autowired
    private IWorkerTypeService workerTypeService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "分页查询", operDesc = "分页列表查询工种信息")
    @ApiOperation(value = " 分页列表查询工种信息", notes = "分页列表查询工种信息", httpMethod = "POST")
    @ApiImplicitParam(name = "engineeringSn", value = "工程sn", paramType = "query", dataType = "String")
    @PostMapping(value = "/page")
    public Result<IPage<WorkerType>> queryPageList(Page page, @RequestBody WorkerType workerType) {
        LambdaQueryWrapper<WorkerType> wrapper = Wrappers.<WorkerType>lambdaQuery();
        if (StringUtils.isNotBlank(workerType.getEngineeringSn())) {
            wrapper.eq(WorkerType::getEngineeringSn, workerType.getEngineeringSn());
        }
        IPage<WorkerType> pageList = workerTypeService.getPageList(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 列表查询
     *
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "列表查询", operDesc = "列表查询工种信息")
    @ApiOperation(value = " 列表查询工种信息", notes = "列表查询工种信息", httpMethod = "POST")
    @ApiImplicitParam(name = "engineeringSn", value = "工程sn", paramType = "query", dataType = "String")
    @PostMapping(value = "/list")
    public Result<List<WorkerType>> queryList(@RequestBody WorkerType workerType) {
        LambdaQueryWrapper<WorkerType> wrapper = Wrappers.<WorkerType>lambdaQuery();
        if (StringUtils.isNotBlank(workerType.getEngineeringSn())) {
            wrapper.eq(WorkerType::getEngineeringSn, workerType.getEngineeringSn());
        }
        List<WorkerType> list = workerTypeService.getList(wrapper);
        return Result.success(list);
    }


    /**
     * 添加
     *
     * @param workerType
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "新增", operDesc = "添加工种信息")
    @ApiOperation(value = " 添加工种信息", notes = "添加工种信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody WorkerType workerType) {
        workerTypeService.save(workerType);
        return Result.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param workerType
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "修改", operDesc = "编辑工种信息")
    @ApiOperation(value = "编辑工种信息", notes = "编辑工种信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<WorkerType> edit(@RequestBody WorkerType workerType) {
        Result<WorkerType> result = new Result<WorkerType>();
        WorkerType workerTypeEntity = workerTypeService.getById(workerType.getId());
        if (workerTypeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = workerTypeService.updateById(workerType);
            if (ok) {
                result.success("修改成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }

    /**
     * 通过id删除
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "删除", operDesc = "删除工种信息")
    @ApiOperation(value = "删除工种信息", notes = "删除工种信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "工种ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/delete")
    public Result<WorkerType> delete(@RequestBody WorkerType workerType) {
        Result<WorkerType> result = new Result<WorkerType>();
        WorkerType workerTypeEntity = workerTypeService.getById(workerType.getId());
        if (workerTypeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = workerTypeService.removeById(workerType.getId());
            if (ok) {
                result.success("删除成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @OperLog(operModul = "工种管理", operType = "查询", operDesc = "通过id查询工种信息")
    @ApiOperation(value = "通过id查询工种信息", notes = "通过id查询工种信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "工种ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<WorkerType> queryById(@RequestBody WorkerType workerTypeEntity) {
        Result<WorkerType> result = new Result<WorkerType>();
        WorkerType workerType = workerTypeService.getById(workerTypeEntity.getId());
        if (workerType == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(workerType);
            result.setSuccess(true);
        }
        return result;
    }
}
