package com.ruoyi.web.controller.warehouse.check;

import java.util.Date;
import java.util.List;

import com.ruoyi.basicData.service.ITWarehouseService;
import com.ruoyi.common.core.domain.entity.TWarehouse;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.warehouse.check.domain.TWarehouseCheckItems;
import com.ruoyi.warehouse.check.domain.TWarehouseCheckItemsDetails;
import com.ruoyi.warehouse.check.service.ITWarehouseCheckItemsDetailsService;
import com.ruoyi.warehouse.check.service.ITWarehouseCheckItemsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warehouse.check.domain.TWarehouseCheck;
import com.ruoyi.warehouse.check.service.ITWarehouseCheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡检checkController
 * 
 * @author dongyongwei
 * @date 2021-07-21
 */
@RestController
@RequestMapping("/warehouse/check")
public class TWarehouseCheckController extends BaseController
{
    @Autowired
    private ITWarehouseCheckService tWarehouseCheckService;

    @Autowired
    private ITWarehouseCheckItemsService itWarehouseCheckItemsService;

    @Autowired
    private ITWarehouseCheckItemsDetailsService tWarehouseCheckItemsDetailsService;

    /**
     * 获取表内存在的仓库列表
     */
    /**
     * 查询巡检check列表
     */
    //@PreAuthorize("@ss.hasPermi('warehouse:check:list')")
    @GetMapping("/selectTWarehouseListInCheck")
    public TableDataInfo list()
    {
//        startPage();
        List<TWarehouse> list = tWarehouseCheckService.selectTWarehouseListInCheck();
        return getDataTable(list);
    }


    /**
     * 查询巡检check列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseCheck tWarehouseCheck)
    {
        startPage();
        List<TWarehouseCheck> list = tWarehouseCheckService.selectTWarehouseCheckList(tWarehouseCheck);
        return getDataTable(list);
    }

    /**
     * 查询巡检check列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:list')")
    @GetMapping("/listByUser")
    public TableDataInfo listByUser(TWarehouseCheck tWarehouseCheck)
    {
        tWarehouseCheck.setfInspectorId(SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<TWarehouseCheck> list = tWarehouseCheckService.selectTWarehouseCheckList(tWarehouseCheck);
        return getDataTable(list);
    }

    /**
     * 导出巡检check列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:export')")
    @Log(title = "巡检check", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseCheck tWarehouseCheck)
    {
        List<TWarehouseCheck> list = tWarehouseCheckService.selectTWarehouseCheckList(tWarehouseCheck);
        ExcelUtil<TWarehouseCheck> util = new ExcelUtil<TWarehouseCheck>(TWarehouseCheck.class);
        return util.exportExcel(list, "check");
    }

    /**
     * 获取巡检check详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tWarehouseCheckService.selectTWarehouseCheckById(fId));
    }

    /**
     * 新增巡检check
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:add')")
    @Log(title = "巡检check", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseCheck tWarehouseCheck)
    {
        return toAjax(tWarehouseCheckService.insertTWarehouseCheck(tWarehouseCheck).intValue());
    }

    /**
     * 新增巡检check
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:add')")
    @Log(title = "巡检saveCheckAndItems", businessType = BusinessType.INSERT)
    @PostMapping("/saveCheckAndItems")
    public AjaxResult saveCheckAndItems(@RequestBody TWarehouseCheck tWarehouseCheck)
    {
        //大计划fId
        Long fId = 0L;
        if (tWarehouseCheck.getfId() != null){
            //更新大计划
            fId = tWarehouseCheck.getfId();
            tWarehouseCheckService.updateTWarehouseCheck(tWarehouseCheck);
        }else{
            //新增大计划
            fId =tWarehouseCheckService.insertTWarehouseCheck(tWarehouseCheck);
        }

        if(fId>0){
            List<TWarehouseCheckItems> tWarehouseCheckItemsList = tWarehouseCheck.gettWarehouseCheckItemsList();
            if(tWarehouseCheckItemsList.size()>0){//有数据情况下
                for (int j = 0; j < tWarehouseCheckItemsList.size(); j++) {
                    TWarehouseCheckItems tWarehouseCheckItems = tWarehouseCheckItemsList.get(j);
                    if(tWarehouseCheckItems.getfId() != null){
                        //更新数据
                        itWarehouseCheckItemsService.updateTWarehouseCheckItems(tWarehouseCheckItems);
                    }else{
                        //新增数据
                        tWarehouseCheckItems.setfCheckFid(fId);
                        itWarehouseCheckItemsService.insertTWarehouseCheckItems(tWarehouseCheckItems);
                    }
                }
            }
            return AjaxResult.success(fId);
        }else{
            return AjaxResult.error("巡检任务更新失败,明细未处理");
        }
    }


    /**
     * 修改巡检check
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:edit')")
    @Log(title = "巡检check", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseCheck tWarehouseCheck)
    {
        return toAjax(tWarehouseCheckService.updateTWarehouseCheck(tWarehouseCheck));
    }

    /**
     * 删除巡检check
     */
    @PreAuthorize("@ss.hasPermi('warehouse:check:remove')")
    @Log(title = "巡检check", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        for (int f = 0; f < fIds.length; f++) {
            Long checkId = fIds[f];
            TWarehouseCheckItemsDetails tWarehouseCheckItemsDetailsQuery = new TWarehouseCheckItemsDetails();
            tWarehouseCheckItemsDetailsQuery.setfCheckFid(checkId);
            List<TWarehouseCheckItemsDetails> tWarehouseCheckItemsDetailsList =tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsList(tWarehouseCheckItemsDetailsQuery);
            if(tWarehouseCheckItemsDetailsList.size()>0){
                return AjaxResult.error("任务编号:"+checkId.intValue()+" 已存在巡检记录,无法删除");
            }
        }
        return toAjax(tWarehouseCheckService.deleteTWarehouseCheckByIds(fIds));
    }
}
