package com.scm.contract.web.purchase;

import com.scm.contract.model.purchase.TagPurchase;
import com.scm.contract.service.purchase.TagPurchaseService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.BaseController;
import com.set.direwolf.core.web.OperationResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zuo
 * @date 2024/6/4 17:43
 */

@Controller
@RequestMapping("${adminPath}/tag/purchase")
public class TagPurchaseController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TagPurchaseController.class);

    private final TagPurchaseService entityService;

    @Autowired
    public TagPurchaseController(TagPurchaseService entityService) {
        this.entityService = entityService;
    }

    /**
     * 获取签采购分页查询信息
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody TagPurchase entity, HttpServletRequest request, HttpServletResponse response) {
        Page<TagPurchase> page = Page.bootstrapPage(request, response);
        List<List<String>> orderList = entity.getOrder();
        String defaultOrder = HumpLineAdopter.humpToLine(orderList);
        if (StringUtils.isNotEmpty(defaultOrder)) {
            page.setOrderBy(defaultOrder);
        }
        return OperationResult.buildSuccessResult("获取签采购分页查询信息成功", entityService.findPage(page, entity));
    }

    /**
     * 获取标签采购详情
     *
     * @param id
     * @return Page
     */
    @GetMapping("/{tagPurchaseId}")
    public @ResponseBody OperationResult getById(@PathVariable("tagPurchaseId") String id) {
        return OperationResult.buildSuccessResult("获取签采购分页查询信息成功", entityService.getById(id, true));
    }


    /**
     * 新建采购
     *
     * @param tagPurchase 参数实体
     * @return Page
     */
    @PostMapping("")
    public @ResponseBody OperationResult saveList(@RequestBody TagPurchase tagPurchase) {
        try {
            entityService.save(tagPurchase);
        } catch (Exception e) {
            logger.error("新建采购失败", e);
            return OperationResult.buildSuccessResult("新建采购失败", false);
        }
        return OperationResult.buildSuccessResult("新建采购成功", true);
    }

    /**
     * 修改采购
     *
     * @param tagPurchase 参数实体
     * @return Page
     */
    @PutMapping("")
    public @ResponseBody OperationResult update(@RequestBody TagPurchase tagPurchase) {
        return OperationResult.buildSuccessResult("修改采购成功", entityService.update(tagPurchase));
    }


    /**
     * 入库
     *
     * @param ids
     * @return
     */
    @PutMapping("/in-storage")
    public @ResponseBody OperationResult inStorage(@RequestBody List<String> ids) {
        try {
            entityService.inStorage(ids);
        } catch (Exception e) {
            logger.error("入库失败", e);
            return OperationResult.buildSuccessResult("入库失败", false);
        }
        return OperationResult.buildSuccessResult("入库成功", true);
    }

    @DeleteMapping("/{id}")
    public @ResponseBody OperationResult deleteById(@PathVariable("id") String id) {
        return OperationResult.buildSuccessResult("删除成功", entityService.deleteById(id));
    }
}
