package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.vo.ItemStageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IItemStageService;
import com.dayou.entity.ItemStage;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.validation.Valid;

import static com.dayou.common.Constants.*;

/**
 * 项目阶段表
 *
 * @author wucl
 * @since 2022-12-08
 */
@RestController
@RequestMapping("itemStage")
@Slf4j
public class ItemStageController extends BaseController {
    @Autowired
    private IItemStageService itemStageService;

    /**
    * 项目阶段表列表
    */
    @GetMapping("")
    public RestResponse<Page<ItemStage>> page(ItemStage itemStage, Page page){
        Page<ItemStage> pages=itemStageService.selectPage(page,itemStage);
        return RestResponse.data(pages);
    }

    /**
     * 项目阶段表详情
     */
    @GetMapping("/{id}")
    public RestResponse<ItemStage> detail(@PathVariable Long id){
        ItemStage xItemStage =itemStageService.detail(id);
        return RestResponse.data(xItemStage);
     }

    /**
     * 项目阶段表新增
     */
    @OperLog(title = "新增项目阶段")
    @PostMapping("")
    public RestResponse<Long> save(@RequestBody @Valid  ItemStageVO itemStageVO) {
        Long ret = itemStageService.add(itemStageVO);
        return RestResponse.success(ret);
    }

    /**
     * 项目阶段表更新
     */
    @OperLog(title = "更新项目阶段列表")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody ItemStageVO itemStageVO) {
        Boolean ret = itemStageService.update(itemStageVO);
        return RestResponse.data(ret);
    }

    /**
     * 项目阶段表删除
     */
    @OperLog(title = "删除项目阶段列表")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = itemStageService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 项目阶段表下拉列表
     */
    @GetMapping("/simpleAll/{itemId}")
    public RestResponse<List<ItemStage>> simpleAll(@PathVariable("itemId") Long itemId){
        LambdaQueryWrapper<ItemStage> lambdaQueryWrapper = new LambdaQueryWrapper<ItemStage>()
        .select(ItemStage::getFooterId,ItemStage::getName).eq(ItemStage::getItemId,itemId);
        List<ItemStage> list= itemStageService.list(lambdaQueryWrapper);
        return RestResponse.data(list);
    }

    /**
     * 项目阶段流程
     * @param itemId
     * @return
     */
    @GetMapping("/list/{itemId}")
    public RestResponse<List<ItemStageVO>> list(@PathVariable("itemId") Long itemId){
        List<ItemStageVO> result = itemStageService.listByItemId(itemId);
        return RestResponse.data(result);
    }

    /**
     * 项目首页流程数据
     * @return
     */
    @DataPermission(department = true,postChild = false,
            ignoreDepartment = {MANAGER_OFFICE,MARKET_DEPARTMENT},
            ignorePost = {CUSTOMER_MANAGER,SALESMAN})
    @GetMapping("/list")
    public RestResponse<List<List<ItemStageVO>>> stageList(ItemStageVO itemStageVO){
        List<List<ItemStageVO>> list = itemStageService.stageList(itemStageVO);
        return RestResponse.data(list);
    }

    /**
     * 阶段确认
     * @param stageId
     * @param overdue
     * @return
     */
    @GetMapping("/confirm")
    public RestResponse<Boolean> confirmation(Long stageId,Boolean overdue){
        Boolean result = itemStageService.confirmation(stageId,overdue);
        return RestResponse.data(result);
    }
}

