package com.pj.project.tb_business_item;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.pj.constants.UserTypeEnum;
import com.pj.project.tb_business.TbBusiness;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import com.pj.current.satoken.StpUserUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;

import javax.servlet.http.HttpServletResponse;


/**
 * Controller: tb_business_item -- 业务作业项
 *
 * @author qzy
 */
@RestController
@RequestMapping("/TbBusinessItem/")
public class TbBusinessItemController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbBusinessItemService tbBusinessItemService;

    /**
     * 改
     */
    @RequestMapping("update")
    public AjaxJson update(TbBusinessItem t) {
        tbBusinessItemService.updateById(t);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
    public AjaxJson getById(String id) {
        TbBusinessItem t = tbBusinessItemService.getById(id);
        return AjaxJson.getSuccessData(t);
    }
    /**
     * 查 - 根据id
     */
    @RequestMapping("confirm")
    public AjaxJson confirm(Long id) {
        if (!StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(),StpUserUtil.getCustomerId())){
            return AjaxJson.getError("无确认权限");
        }
         tbBusinessItemService.confirmBusinessItem(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        if (StrUtil.isEmpty(so.getString("businessId"))){
            so.put("typeFlag",1);
        }
        String customerId = StpUserUtil.getCustomerId();
        if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("pickCustomerId", customerId);
        }
        List<TbBusinessItem> list = tbBusinessItemService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }



    @RequestMapping("export/items")
    public AjaxJson exportItems(HttpServletResponse response) throws Exception{
        SoMap so = SoMap.getRequestSoMap();
        so.put("typeFlag",1);
        String customerId = StpUserUtil.getCustomerId();
        if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("pickCustomerId", customerId);
        }
        String result = tbBusinessItemService.exportItems(so);
        return AjaxJson.getSuccessData(result);
    }




}
