package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.StaDirector;
import com.liang.service.StaDirectorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * 负责人(StaDirector)表控制层
 *
 * @author makejava
 * @since 2023-04-09 21:54:44
 */
@Controller
@RequestMapping("/staDirector")
@Api(value = "项目负责人管理控制器", tags = "项目负责人管理控制器")
public class StaDirectorController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(StaDirectorController.class);
    /**
     * 服务对象
     */
    @Resource
    private StaDirectorService staDirectorService;

    /**
     * 分页查询
     *
     * @param staDirector 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<StaDirector>> queryByPage(StaDirector staDirector, PageRequest pageRequest) {
        return ResponseEntity.ok(this.staDirectorService.queryByPage(staDirector, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<StaDirector> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.staDirectorService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param staDirector 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<StaDirector> add(StaDirector staDirector) {
        return ResponseEntity.ok(this.staDirectorService.insert(staDirector));
    }

    /**
     * 编辑数据
     *
     * @param staDirector 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<StaDirector> edit(StaDirector staDirector) {
        return ResponseEntity.ok(this.staDirectorService.update(staDirector));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.staDirectorService.deleteById(id));
    }

    /**
     * 批量删除系统用户
     *
     * @return
     */
    @ApiOperation(value = "批量删除项目负责人", notes = "批量删除项目负责人")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchDelete(@RequestBody String data) {
        if (logger.isInfoEnabled()) {
            logger.info("batchDelete -- data：" + data);
        }
        List<Integer> idList = JsonTool.parseArray(data, Integer.class);
        if (logger.isInfoEnabled()) {
            logger.info("batchDelete -- idList：{}", idList);
        }

        int num = staDirectorService.batchDelete(idList, getSysUserId());
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }

    /**
     * 批量保存负责人
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{zb}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zb) {
        //删除项目人员
        staDirectorService.deleteByZbId(zb);

        List<StaDirector> memberList = JsonTool.parseArray(data, StaDirector.class);
        if (memberList.size() > 0) {
            int num = staDirectorService.insertBatch(memberList);
            if (num == memberList.size()) {
                return BaseResult.success("项目成员保存成功！");
            } else {
                return BaseResult.failure("项目成员保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }
}

