package com.pj.project.tb_goods;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxError;
import com.pj.utils.sg.AjaxJson;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Controller: tb_goods -- 商品管理
 * @author qzy 
 */
@RestController
@RequestMapping("/TbGoods/")
public class TbGoodsController {

	/** 底层 Service 对象 */
	@Autowired
	TbGoodsService tbGoodsService;

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

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

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

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	public AjaxJson getList() {
		SoMap so = SoMap.getRequestSoMap();
		List<TbGoods> list = tbGoodsService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}
	
	
	
	/** 改 - 状态(1=禁用,2=启用) */  
	@RequestMapping("updateStatus")
	@SaCheckPermission(TbGoods.PERMISSION_CODE)
	public AjaxJson updateStatus(Long id, String value){
		int line = SP.publicMapper.updateColumnById(TbGoods.TABLE_NAME, "status", value, id);
		return AjaxJson.getByLine(line);
	}
	
	
	// ------------------------- 前端接口 -------------------------
	
	
	/** 改 - 不传不改 [G] */
	@RequestMapping("updateByNotNull")
	public AjaxJson updateByNotNull(Long id){
		AjaxError.throwBy(true, "如需正常调用此接口，请删除此行代码");
		// 鉴别身份，是否为数据创建者 
		long userId = SP.publicMapper.getColumnByIdToLong(TbGoods.TABLE_NAME, "user_id", id);
		AjaxError.throwBy(userId != StpUserUtil.getLoginIdAsLong(), "此数据您无权限修改");
		// 开始修改 (请只保留需要修改的字段)
		SoMap so = SoMap.getRequestSoMap();
		so.clearNotIn("id", "code", "name", "status", "createTime").clearNull().humpToLineCase();	
		int line = SP.publicMapper.updateBySoMapById(TbGoods.TABLE_NAME, so, id);
		return AjaxJson.getByLine(line);
	}
	
	
	
	
	
	

}
