package com.jsh.erp.controller.stocktaking;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jsh.erp.base.AjaxResult;
import com.jsh.erp.base.BaseController;
import com.jsh.erp.base.TableDataInfo;
import com.jsh.erp.datasource.dto.TaskStocktakingDTO;
import com.jsh.erp.datasource.dto.TaskStocktakingItemDTO;
import com.jsh.erp.datasource.entities.TaskStocktaking;
import com.jsh.erp.datasource.entities.TaskStocktakingItem;
import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.vo.SpinnerVO;
import com.jsh.erp.datasource.vo.TaskStocktakingVO;
import com.jsh.erp.service.TaskStocktakingItemService;
import com.jsh.erp.service.TaskStocktakingService;
import com.jsh.erp.service.UserService;
import com.jsh.erp.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping(value = "/stocktaking")
@Api(tags = {"盘点接口"})
public class StocktakingController extends BaseController {

    @Resource
    private TaskStocktakingService taskStocktakingService;

    @Resource
    private UserService userService;

    @Resource
    private TaskStocktakingItemService taskStocktakingItemService;

    @ApiOperation("盘点任务列表")
    @PostMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<TaskStocktakingVO> list = taskStocktakingService.listBy();
        return getDataTable(list);
    }

    @ApiOperation("新增盘点任务")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TaskStocktakingDTO taskStocktakingDTO) {
        boolean b = taskStocktakingService.add(taskStocktakingDTO);
        if (!b){
            return AjaxResult.error("创建失败，请联系系统管理员");
        }
        return AjaxResult.success();
    }

    @ApiOperation("负责人下拉列表")
    @GetMapping("/creatorSpinnerList")
    public AjaxResult creatorSpinnerList() {
        List<SpinnerVO> spinnerVOList = userService.creatorSpinnerList();
        return AjaxResult.success(spinnerVOList);
    }

    /**
     * 查询任务详情
     * @param id
     * @return
     */
    @ApiOperation("任务详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) throws Exception{
        return AjaxResult.success(taskStocktakingService.detail(id));
    }

    /**
     * 任务详情-商品列表
     * @param taskStocktakingId 任务ID
     * @return
     */
    @ApiOperation("任务详情-商品列表")
    @GetMapping("/detailByItemList/{taskStocktakingId}")
    public AjaxResult detailByItemList(@PathVariable("taskStocktakingId") Long taskStocktakingId) {
        return AjaxResult.success(taskStocktakingService.listByTaskStocktakingId(taskStocktakingId));
    }

    @ApiOperation("任务详情-修改")
    @PostMapping("/detailUpdate")
    public AjaxResult detailUpdate(@RequestBody TaskStocktakingDTO taskStocktakingDTO) {
        boolean b = taskStocktakingService.detailUpdate(taskStocktakingDTO);
        if (!b){
            return AjaxResult.error("修改失败，请联系系统管理员");
        }
        return AjaxResult.success();
    }

    /**
     * 任务详情-商品列表-编辑
     * @return
     */
    @ApiModelProperty("任务详情-商品-编辑")
    @PostMapping("/itemUpdate")
    public AjaxResult itemUpdate(@RequestBody TaskStocktakingItemDTO taskStocktakingItemDTO) throws Exception {
        User currentUser = userService.getCurrentUser();
        taskStocktakingItemService.update(new UpdateWrapper<TaskStocktakingItem>()
                .set("new_inventory", taskStocktakingItemDTO.getNewInventory())
                .set("new_position", taskStocktakingItemDTO.getNewPosition())
                .set("difference_count", taskStocktakingItemDTO.getDifferenceCount())
                .set("difference_reason", taskStocktakingItemDTO.getDifferenceReason())
                .set("creator", currentUser.getId())
                .set("oper_time", DateUtils.getTime())
                .eq("id", taskStocktakingItemDTO.getId()));
        return AjaxResult.success();
    }

    /**
     * 任务详情-商品-删除
     * @param id 商品ID
     * @return
     */
    @ApiModelProperty("任务详情-商品-删除")
    @GetMapping("/itemDelete/{id}")
    public AjaxResult itemDelete(@PathVariable("id") Long id) {
        taskStocktakingItemService.update(new UpdateWrapper<TaskStocktakingItem>().set("delete_flag", true).eq("id", id));
        return AjaxResult.success();
    }

    @ApiOperation("取消任务")
    @GetMapping("/taskCancel/{ids}")
    public AjaxResult taskCancel(@PathVariable("ids") Long[] ids) {
        Arrays.asList(ids).forEach(id -> {
            taskStocktakingService.update(new UpdateWrapper<TaskStocktaking>().eq("id", id).set("task_status", "4"));
        });
        return AjaxResult.success();
    }

}
