package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.annotation.Transfer;
import com.dayou.dto.BusinessOpportunityDTO;
import com.dayou.dto.CustomerDTO;
import com.dayou.enums.BusinessResourceEnum;
import com.dayou.vo.BusinessOpportunityVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.controller.BaseController;
import com.dayou.service.IBusinessOpportunityService;
import com.dayou.entity.BusinessOpportunity;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 商机
 *
 * @author wucl
 * @since 2023-01-10
 */
@RestController
@RequestMapping("businessOpportunity")
@Slf4j
public class BusinessOpportunityController extends BaseController {
    @Autowired
    private IBusinessOpportunityService businessOpportunityService;

    /**
    * 商机列表
    */
    @DataPermission(teamData = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("")
    public RestResponse<Page<BusinessOpportunityVO>> page(BusinessOpportunityVO businessOpportunity, Page page,String keyword){
        Page<BusinessOpportunityVO> pages=businessOpportunityService.selectPage(page,businessOpportunity,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 商机详情
     */
    @GetMapping("/{id}")
    public RestResponse<BusinessOpportunity> detail(@PathVariable Long id){
        BusinessOpportunity xBusinessOpportunity =businessOpportunityService.detail(id);
        return RestResponse.data(xBusinessOpportunity);
     }

    /**
     * 商机新增
     */
    @OperLog(title = "新增商机")
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody BusinessOpportunity businessOpportunity) {
        Boolean ret = businessOpportunityService.add(businessOpportunity);
        return RestResponse.data(ret);
    }

    /**
     * 商机更新
     */
    @OperLog(title = "更新商机")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody BusinessOpportunity businessOpportunity) {
        Boolean ret = businessOpportunityService.update(businessOpportunity);
        return RestResponse.data(ret);
    }

    /**
     * 商机删除
     */
    @OperLog(title = "删除商机")
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = businessOpportunityService.delete(id);
        return RestResponse.data(ret);
    }

    @Transfer(resourceEnum = BusinessResourceEnum.BUSINESS_OPPORTUNITY)
    @OperLog(title = "更新客户经理")
    @PutMapping("/changeManager")
    public RestResponse<Boolean> changeManager(@RequestBody BusinessOpportunityDTO opportunity){
        Boolean result = businessOpportunityService.changeManager(opportunity);
        return RestResponse.data(result);
    }

}

