package com.ghy.web.controller.worker;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.worker.domain.WorkerTeam;
import com.ghy.worker.service.WorkerTeamService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/worker/team")
public class WorkerTeamController extends BaseController {

    @Resource
    private WorkerTeamService workerTeamService;

    @PostMapping("/addTeam")
    @ResponseBody
    public AjaxResult addTeam(@RequestBody WorkerTeam workerTeam){
        try {
            List<WorkerTeam> workerTeams = workerTeamService.getWorkerTeam(workerTeam);
            if (CollectionUtils.isNotEmpty(workerTeams)) {
                return AjaxResult.warn("已加入，请不要重复操作");
            }

            int result = workerTeamService.insertWorkerTeam(workerTeam);
            if(result > 0){
                return AjaxResult.success("新增成功");
            }else {
                return AjaxResult.warn("新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }

    @PostMapping("/getTeamList")
    @ResponseBody
    public TableDataInfo getTeamList(@RequestBody WorkerTeam workerTeam){
        startPage();
        List<WorkerTeam> list = workerTeamService.getWorkerTeam(workerTeam);
        return getDataTable(list);
    }

    @PostMapping("/updateTeam")
    @ResponseBody
    public AjaxResult updateTeam(@RequestBody WorkerTeam workerTeam){
        try {
            int result = workerTeamService.updateWorkerTeam(workerTeam);
            if(result > 0){
                return AjaxResult.success("修改成功");
            }else {
                return AjaxResult.warn("修改失败");
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }
}

