package com.pj.project.tb_fee_details;

import java.math.BigDecimal;
import java.util.List;

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_fee_details -- 收费明细表
 * @author lzm 
 */
@RestController
@RequestMapping("/TbFeeDetails/")
public class TbFeeDetailsController {

	/** 底层 Service 对象 */
	@Autowired
	TbFeeDetailsService tbFeeDetailsService;

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

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

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

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		List<TbFeeDetails> list = tbFeeDetailsService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

    /** 导出日收费明细excel */
    @RequestMapping("export/details")
    public AjaxJson exportDetails() throws Exception {
	    SoMap so = SoMap.getRequestSoMap();
	    return AjaxJson.getSuccessData(tbFeeDetailsService.export(so));
    }

    @RequestMapping("calcuAgain")
    public AjaxJson calcuAgain(){
        List<TbFeeDetails> list = tbFeeDetailsService.list();
        for (TbFeeDetails detail : list) {
            BigDecimal taxPrice = BigDecimal.valueOf(0);
            BigDecimal noTaxPrice = BigDecimal.valueOf(0);
            taxPrice = detail.getItemPrice().divide(BigDecimal.valueOf(1).add(detail.getTaxRate()),2, BigDecimal.ROUND_HALF_UP).multiply(detail.getTaxRate());
            taxPrice = taxPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
            noTaxPrice = detail.getItemPrice().subtract(taxPrice);
            noTaxPrice = noTaxPrice.setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setTaxPrice(taxPrice).setNoTaxPrice(noTaxPrice);
        }
        tbFeeDetailsService.updateBatchById(list);
        return AjaxJson.getSuccess();
    }
	
	
	
	// ------------------------- 前端接口 -------------------------
	
	
	/** 改 - 不传不改 [G] */
	@RequestMapping("updateByNotNull")
	public AjaxJson updateByNotNull(Long id){
		AjaxError.throwBy(true, "如需正常调用此接口，请删除此行代码");
		// 鉴别身份，是否为数据创建者 
		long userId = SP.publicMapper.getColumnByIdToLong(TbFeeDetails.TABLE_NAME, "user_id", id);
		AjaxError.throwBy(userId != StpUserUtil.getLoginIdAsLong(), "此数据您无权限修改");
		// 开始修改 (请只保留需要修改的字段)
		SoMap so = SoMap.getRequestSoMap();
		so.clearNotIn("id", "businessNo", "carNo", "itemTypeId", "itemTypeName", "itemId", "itemName", "itemPrice", "payDay", "payType", "createTime").clearNull().humpToLineCase();	
		int line = SP.publicMapper.updateBySoMapById(TbFeeDetails.TABLE_NAME, so, id);
		return AjaxJson.getByLine(line);
	}
	
	
	
	
	
	

}
