package com.pj.project.tb_disinfect;

import java.util.List;

import com.pj.constants.UserTypeEnum;
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;


/**
 * Controller: tb_disinfect -- 消毒申请单
 * @author qzy 
 */
@RestController
@RequestMapping("/TbDisinfect/")
public class TbDisinfectController {

	/** 底层 Service 对象 */
	@Autowired
	TbDisinfectService tbDisinfectService;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(TbDisinfect.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(TbDisinfect t){
		tbDisinfectService.add(t);
		t = tbDisinfectService.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(t);
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(TbDisinfect.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		tbDisinfectService.delete(id);
		return AjaxJson.getSuccess();
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(TbDisinfect.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
		int line = SP.publicMapper.deleteByIds(TbDisinfect.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(TbDisinfect.PERMISSION_CODE)
	public AjaxJson update(TbDisinfect t){
		tbDisinfectService.update(t);
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
	public AjaxJson getById(Long id){
		TbDisinfect t = tbDisinfectService.getById(id);
		return AjaxJson.getSuccessData(t);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
        String currentCustomerId = StpUserUtil.getCustomerId();
        if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("customerId", currentCustomerId);
        }
		List<TbDisinfect> list = tbDisinfectService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}
    @RequestMapping("export/disinfect")
    public AjaxJson exportDisinfect() throws Exception{
        SoMap so = SoMap.getRequestSoMap();
	    return AjaxJson.getSuccessData(tbDisinfectService.exportDisinfect(so));
    }
	
	
	
	
	// ------------------------- 前端接口 -------------------------
	
	
	/** 改 - 不传不改 [G] */
	@RequestMapping("updateByNotNull")
	public AjaxJson updateByNotNull(Long id){
		AjaxError.throwBy(true, "如需正常调用此接口，请删除此行代码");
		// 鉴别身份，是否为数据创建者 
		long userId = SP.publicMapper.getColumnByIdToLong(TbDisinfect.TABLE_NAME, "user_id", id);
		AjaxError.throwBy(userId != StpUserUtil.getLoginIdAsLong(), "此数据您无权限修改");
		// 开始修改 (请只保留需要修改的字段)
		SoMap so = SoMap.getRequestSoMap();
		so.clearNotIn("id", "sendPeople", "receivePeople", "goodsName", "origin", "num", "grossWeight", "unit", "pack", "flag", "phone", "sourceAddress", "arrivePart", "carName", "carNo", "chinaCarName", "chinaCarNo", "remark", "applyUnit", "applyPeople", "applyPhone", "applyTime", "acceptPeople", "acceptTime", "money").clearNull().humpToLineCase();	
		int line = SP.publicMapper.updateBySoMapById(TbDisinfect.TABLE_NAME, so, id);
		return AjaxJson.getByLine(line);
	}
	
	
	
	
	
	

}
