package com.jeeplus.business.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.assess.program.configuration.projectList.service.dto.ContractDto;
import com.jeeplus.assess.program.configuration.projectList.service.dto.ProjectListDto;
import com.jeeplus.assess.workContract.domain.WorkContractInfo;
import com.jeeplus.business.project.domain.JyProject;
import com.jeeplus.business.project.service.JyProjectService;
import com.jeeplus.business.project.service.dto.JyProjectDTO;
import com.jeeplus.business.workContractInfo.domain.JyWorkContractInfo;
import com.jeeplus.business.workContractInfo.service.dto.JyWorkContractInfoDto;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@Api(tags ="嘉溢项目登记")
@RestController
@RequestMapping(value = "/jyProject")
public class JyProjectController {

    @Resource
    private JyProjectService jyProjectService;


    /**
     * 项目登记列表
     * @param info
     * @param page
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "项目登记列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<JyProject>> list(JyProject info, Page<JyProject> page) throws Exception {
        IPage<JyProject> list = jyProjectService.list(page, info);
        return ResponseEntity.ok (list);
    }

    /**
     * 查询当前登录人所属项目组的项目
     * @return
     */
    @ApiOperation(value = "查询当前登录人所属项目组的项目")
    @GetMapping(value = "/getHaveProjectIds")
    public ResponseEntity<List<String>> getHaveProjectIds() {
        return jyProjectService.getHaveProjectIds();
    }

    /**
     * 合同信息列表查询
     * @param dto
     * @param page
     * @return
     */
    @ApiOperation(value = "合同信息列表查询")
    @GetMapping(value = "/findContractPageList")
    public ResponseEntity<IPage<JyWorkContractInfo>> findContractPageList(JyWorkContractInfoDto dto, Page<JyWorkContractInfo> page) {
        IPage<JyWorkContractInfo> pageList = jyProjectService.findContractPageList(dto, page);
        return ResponseEntity.ok(pageList);
    }

    /**
     * 流程保存
     */
    @ApiOperation(value = "流程保存")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody JyProjectDTO jyProjectDTO) throws Exception{
        String id = jyProjectService.save(jyProjectDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_record").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/findById")
    public ResponseEntity<JyProjectDTO> save(@RequestParam String id){
        JyProjectDTO dto = jyProjectService.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody JyProjectDTO jyProjectDTO) {
        jyProjectService.updateStatusById(jyProjectDTO);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @GetMapping(value = "/deleteById")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = jyProjectService.deleteById(id);
        return ResponseEntity.ok(s);
    }


    /**
     * 批量删除字典
     */
    @DemoMode
    @ApiLog("批量删除项目信息")
    @PreAuthorize("hasAuthority('jy_project:info:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        String idArray[] = ids.split ( "," );
        for(String id: idArray){
            jyProjectService.deleteById ( id );
        }
        return ResponseEntity.ok ( "删除信息成功！" );
    }














}
