/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2021-07-12
 * 描述            ：成本月份-周成本管理
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Cm.mapper.CmMonthBaseCostMapper;
import market.Cm.service.CmMonthBaseCostService;
import market.Cm.vo.CmMonthBaseCost;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.vo.YdmProductDetail;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="CmMonthBaseCostService")
public class CmMonthBaseCostServiceImpl extends BaseServiceImpl<CmMonthBaseCost,Object> implements CmMonthBaseCostService {
	
	/**
	 * 周成本报表：查询
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmMonthBaseCost(CommonPage<CmMonthBaseCost> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmMonthBaseCost cmbp = cp.getObject();		 
		try {
			String swhere = "";			
			if (cmbp.getPrd_num()!=null && !"".equals(cmbp.getPrd_num())) {
				swhere+=" and prd_num like '%"+cmbp.getPrd_num()+"%'";//成本月份
			}
			List<CmMonthBaseCost> list = this.getMapper(CmMonthBaseCostMapper.class).findWithCondition(null, swhere, "prd_num desc,week_no", 0, 0);
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 周成本报表：保存
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertCmMonthBaseCost(CommonPage<CmMonthBaseCost> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		ResultModel rm = new ResultModel();
		List<CmMonthBaseCost> cmbcList = cp.getObjectList();
		
		//获取当前月份的天数
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(df.parse(cmbcList.get(0).getPrd_num()+"-01"));
        int daysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//天数
        
        //1. 删除当前月份所有的数据
        String sql = "delete from cm_month_base_cost t where t.prd_num = '"+cmbcList.get(0).getPrd_num()+"'";
        this.getMapper(CmMonthBaseCostMapper.class).doDeleteCmMonthBaseCost(sql);
        
        //2. 执行新增
		for (CmMonthBaseCost cmbc : cmbcList) {		
			if (StringUtils.isEmpty(cmbc.getPrd_num())) {
				throw new Exception("月份不能为空");
			}
			//必传月份2021-07，第几周(1,2,3,4)，
			//根据第几周获取开始日期和结束日期
			if ("1".equals(cmbc.getWeek_no())) {
				cmbc.setWeek_start(cmbc.getPrd_num()+"-01");
				cmbc.setWeek_end(cmbc.getPrd_num()+"-07");
			}else if ("2".equals(cmbc.getWeek_no())) {
				cmbc.setWeek_start(cmbc.getPrd_num()+"-08");
				cmbc.setWeek_end(cmbc.getPrd_num()+"-14");
			}else if ("3".equals(cmbc.getWeek_no())) {
				cmbc.setWeek_start(cmbc.getPrd_num()+"-15");
				cmbc.setWeek_end(cmbc.getPrd_num()+"-21");
			}else if ("4".equals(cmbc.getWeek_no())) {
				//第4周是22号到月末
				cmbc.setWeek_start(cmbc.getPrd_num()+"-22");
				cmbc.setWeek_end(cmbc.getPrd_num()+"-"+daysOfMonth);
			}
			cmbc.setCreate_man(userName);
			cmbc.setCreate_time(now);
			this.getMapper(CmMonthBaseCostMapper.class).insert(cmbc);
		}
		
		rm.setState("200");
		rm.setMsgInfo("保存成功");
		return rm;
	}
	
	/**
	 * 周成本产品成本明细表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public ResultModel doQueryWeekCostDetail(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		YdmProductDetail ypd = cp.getObject();
		String startTime = cp.getStartTime().trim();
		String endTime = cp.getEndTime().trim();
		try {
			String swhere = " and t.storageattr = '500201' and length(t.coilno) = 8";			
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno().trim())) {
				swhere+=" and t.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";//原料卷号
			}
			if (StringUtils.isNotEmpty(startTime)) {
				swhere += " and t.createdate >= to_date('"+startTime+"', 'yyyy-MM-dd HH24:mi:ss')";
			}
			if (StringUtils.isNotEmpty(endTime)) {
				swhere += " and t.createdate <= to_date('"+endTime+"', 'yyyy-MM-dd HH24:mi:ss')";
			}
			List<HashMap> list = this.getMapper(CmMonthBaseCostMapper.class).doQueryWeekCostDetail(swhere);
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	
	@SuppressWarnings("rawtypes")
	public ResultModel doQueryWeekSalePross(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		YdmProductDetail ypd = cp.getObject();
		String startTime = cp.getStartTime().trim();
		String endTime = cp.getEndTime().trim();
		try {
			String swhere = " and soi.balance_price > 0";			
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno().trim())) {
				swhere+=" and slbd.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";//原料卷号
			}
			if (StringUtils.isNotEmpty(startTime)) {
				swhere += " and t.loaddate >= to_date('"+startTime+"', 'yyyy-MM-dd HH24:mi:ss')";
			}
			if (StringUtils.isNotEmpty(endTime)) {
				swhere += " and t.loaddate <= to_date('"+endTime+"', 'yyyy-MM-dd HH24:mi:ss')";
			}
			List<HashMap> list = this.getMapper(CmMonthBaseCostMapper.class).doQueryWeekSalePross(swhere);
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
}
