package com.dayou.controller;

import com.dayou.annotation.DataPermission;
import com.dayou.annotation.OperLog;
import com.dayou.annotation.Transfer;
import com.dayou.brokerage.annotation.DoBrokerage;
import com.dayou.dto.ItemDTO;
import com.dayou.dto.ItemStateUpdateDTO;
import com.dayou.enums.BusinessResourceEnum;
import com.dayou.utils.LoginContext;
import com.dayou.vo.DepartmentStatVO;
import com.dayou.vo.ItemStatVO;
import com.dayou.vo.ItemVO;
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.IItemService;
import com.dayou.entity.Item;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.HttpKit;
import com.dayou.exception.ErrorCode;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static com.dayou.common.Constants.MANAGER_OFFICE;
import static com.dayou.common.Constants.MARKET_DEPARTMENT;
import static com.dayou.enums.OperationTypeEnum.ADD;

/**
 * 项目信息表
 *
 * @author wucl
 * @since 2022-12-08
 */
@RestController
@RequestMapping("item")
@Slf4j
public class ItemController extends BaseController {
    @Autowired
    private IItemService itemService;

    /**
    * 项目信息表列表（项目所属部门是自己的部门的项目）
    */
    @DataPermission(department = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("")
    public RestResponse<Page<ItemVO>> page(ItemDTO item, Page page){
        Page<ItemVO> pages=itemService.xSelectPage(page,item);
        return RestResponse.data(pages);
    }

    /**
     * 项目信息表列表（项目所属部门是自己的部门的项目）导出
     */
    @DataPermission(department = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("/export")
    public void exportData(ItemDTO item, HttpServletResponse response) throws IOException {
        List<ItemVO> list = itemService.xList(item);
        exportPlus(response,"项目列表",list,ItemVO.class);
    }

    /**
     * 市场部项目列表 (包含客户经理是自己或客户经理是自己下属的项目)
     */
    @DataPermission(teamData = true,postChild = false,ignoreDepartment = {MANAGER_OFFICE})
    @GetMapping("/mine")
    public RestResponse<Page<ItemVO>> xPage(ItemDTO item, Page page){
        Page<ItemVO> pages=itemService.selectPage(page,item);
        return RestResponse.data(pages);
    }

    /**
     * 市场部项目列表 (包含客户经理是自己或客户经理是自己下属的项目) 导出
     */
    @DataPermission(teamData = true,postChild = false)
    @GetMapping("/mine/export")
    public void exportDataMine(ItemDTO item, HttpServletResponse response) throws IOException {
        List<ItemVO> list = itemService.xMineList(item);
        exportPlus(response,"项目列表",list,ItemVO.class);
    }

    /**
     * 市场部我的项目列表 (只包含客户经理是自己的项目)
     */
    @GetMapping("/me")
    public RestResponse<Page<ItemVO>> xxPage(ItemDTO item, Page page){
        HashSet<Long> userIds = new HashSet<>();
        userIds.add(LoginContext.getUserId());
        item.setUserIds(userIds);
        Page<ItemVO> pages=itemService.selectPage(page,item);
        return RestResponse.data(pages);
    }
    /**
     * 项目信息表详情
     */
    @GetMapping("/{id}")
    public RestResponse<ItemVO> detail(@PathVariable Long id){
        ItemVO itemVO =itemService.detail(id);
        return RestResponse.data(itemVO);
     }

    /**
     * 项目信息表新增
     */
    @OperLog(title = "新增项目")
    @PostMapping("")
    public RestResponse<Long> save(@RequestBody ItemDTO itemDTO) {
        Long ret = itemService.add(itemDTO);
        return RestResponse.success(ret);
    }

    /**
     * 项目信息表更新
     */
    @OperLog(title = "更新项目")
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody ItemDTO itemDTO) {
        Boolean ret = itemService.update(itemDTO);
        return RestResponse.data(ret);
    }

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


    /**
     * 项目信息表下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<Item> lambdaQueryWrapper = new LambdaQueryWrapper<Item>()
        .select(Item::getId,Item::getName)
        .eq(Item::getDeleted,false);
        List<Item> list= itemService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 统计
     * @return
     */
    @GetMapping("/stat")
    public RestResponse<ItemStatVO> stat() {
        ItemStatVO bo = itemService.stat();
        return RestResponse.data(bo);
    }

    /**
     * 项目转交
     * @param itemDTO
     * @return
     */
    @Transfer(resourceEnum = BusinessResourceEnum.ITEM)
    @OperLog(title = "更新客户经理")
    @PutMapping("/changeManager")
    public RestResponse<Boolean> changManager(@RequestBody ItemDTO itemDTO){
        Boolean result = itemService.changManager(itemDTO);
        return  RestResponse.data(result);
    }

    /**
     * 项目业务状态变更
     * @return
     */
    @PutMapping("/updateState")
    public RestResponse<Boolean> updateState(@RequestBody @Valid ItemStateUpdateDTO updateDTO){
        Boolean result = itemService.updateState(updateDTO);
        return  RestResponse.data(result);
    }

}

