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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.dto.TeamInfoDto;
import com.zhgd.xmgl.modules.basicdata.entity.TeamInfo;
import com.zhgd.xmgl.modules.basicdata.service.ITeamInfoService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
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.*;

import java.util.List;


/**
 * @Title: Controller
 * @Description: 班组
 * @author： pengj
 * @date： 2023-03-13
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/teamInfo")
@Slf4j
@Api(tags = "班组管理")
public class TeamInfoController {
    @Autowired
    private ITeamInfoService teamInfoService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "班组管理", operType = "分页查询", operDesc = "分页列表查询班组信息")
    @ApiOperation(value = " 分页列表查询班组信息", notes = "分页列表查询班组信息", httpMethod = "POST")
    @ApiImplicitParam(name = "teamName", value = "班组名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/page")
    public Result<IPage<TeamInfoDto>> queryPageList(Page page, @RequestBody TeamInfo teamInfo) {
        QueryWrapper<TeamInfo> wrapper = Wrappers.<TeamInfo>query();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(teamInfo.getTeamName())) {
            wrapper.like("t.teamName", teamInfo.getTeamName());
        }
        /*if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            wrapper.eq("t.engineering_sn", user.getEngineeringSn());
        } else {
            wrapper.eq("t.project_sn", user.getSn()).eq("t.engineering_sn", "");
        }*/
        IPage<TeamInfoDto> pageList = teamInfoService.getPageList(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 列表查询
     * @return
     */
    @OperLog(operModul = "班组管理", operType = "列表查询", operDesc = "列表查询班组信息")
    @ApiOperation(value = " 列表查询班组信息", notes = "列表查询班组信息", httpMethod = "GET")
    @GetMapping(value = "/list")
    public Result<List<TeamInfo>> queryList() {
        LambdaQueryWrapper<TeamInfo> wrapper = Wrappers.<TeamInfo>lambdaQuery();
        /*SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            wrapper.eq(TeamInfo::getEngineeringSn, user.getEngineeringSn());
        } else {
            wrapper.eq(TeamInfo::getProjectSn, user.getSn()).eq(TeamInfo::getEngineeringSn, "");
        }*/
        List<TeamInfo> list = teamInfoService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 添加
     *
     * @param teamInfo
     * @return
     */
    @OperLog(operModul = "班组管理", operType = "新增", operDesc = "添加班组信息")
    @ApiOperation(value = " 添加班组信息", notes = "添加班组信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody TeamInfo teamInfo) {
        SecurityUser user = SecurityUtil.getUser();
        teamInfo.setEngineeringSn(user.getEngineeringSn());
        teamInfo.setProjectSn(user.getSn());
        teamInfoService.saveInfo(teamInfo);
        return Result.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param teamInfo
     * @return
     */
    @OperLog(operModul = "班组管理", operType = "修改", operDesc = "编辑班组信息")
    @ApiOperation(value = "编辑班组信息", notes = "编辑班组信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<TeamInfo> edit(@RequestBody TeamInfo teamInfo) {
        Result<TeamInfo> result = new Result<TeamInfo>();
        TeamInfo teamInfoEntity = teamInfoService.getById(teamInfo.getId());
        if (teamInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            SecurityUser user = SecurityUtil.getUser();
            teamInfo.setEngineeringSn(user.getEngineeringSn());
            teamInfo.setProjectSn(user.getSn());
            boolean ok = teamInfoService.updateById(teamInfo);
            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<TeamInfo> delete(@RequestBody TeamInfo teamInfo) {
        Result<TeamInfo> result = new Result<TeamInfo>();
        TeamInfo teamInfoEntity = teamInfoService.getById(teamInfo.getId());
        if (teamInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = teamInfoService.removeById(teamInfo.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<TeamInfo> queryById(@RequestBody TeamInfo teamInfo) {
        Result<TeamInfo> result = new Result<TeamInfo>();
        TeamInfo teamInfoEntity = teamInfoService.getById(teamInfo.getId());
        if (teamInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(teamInfoEntity);
            result.setSuccess(true);
        }
        return result;
    }
}
