package com.yc.wms.service.impl;

import com.yc.wms.bean.Liaoxiangjiankong;
import com.yc.wms.bean.Pandianming;
import com.yc.wms.service.Pandianmingxi;
import com.yc.wms.until.QueryPageUtil;
import com.yc.wms.until.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PandianmingxiImpl implements Pandianmingxi {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Result QueryJson(QueryPageUtil query, Pandianming pandianming) {

        int pageNum = query.getPageNum();//当前页
        int pageSize = query.getPageSize();//条数

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String sql="select Row_number() OVER(ORDER BY cr.id) as Row_Num, cr.id,cp.CODE cp_code,cr.count_Code,\n" +
                "i.code i_code,i.NAME i_name,i.UNIT,cr.QUANTITY ,\n" +
                "cr.COUNT_QUANTITY ,cr.DELTA_QUANTITY ,lc.code lc_code,\n" +
                "z.NAME z_name,cr.targetArea ,w1.NAME w1_name,w.NAME w_name,\n" +
                "cr.barCode ,ik.PROP_C1 ,ik.PROP_D2 ,ik.PROP_C2 ,\n" +
                "ik.PROP_C3 ,ik.PROP_C5 ,ik.PROP_C9 ,cr.issued_Date ,\n" +
                "cr.Open_Date  ,\n" +
                "cr.end_Date  ,\n" +
                "case WHEN cr.DELTA_QUANTITY =0 then 0 else 1 end as sfcy ,cr.NEED_COUNT \n" +
                " from COUNT_RECORD cr\n" +
                " left join COUNT_PLAN cp on cp.id=cr.COUNT_PLAN_ID\n" +
                " left join LOCATION lc on lc.id=cr.LOCATION_ID\n" +
                " left join zone z on z.id=lc.ZONE_ID\n" +
                " left join worker w on w.id=cr.WORKER_ID\n" +
                " left join worker w1 on w1.id=cr.WORKER1_ID\n" +
                " left join ITEM_KEY ik on ik.id=cr.ITEM_KEY_ID\n" +
                " left join item i on i.id=ik.ITEM_ID\n" +
                " where cr.count_Code is not NULL ";


        String sqlCount = "select count(t.id) as nums from (" + sql+") t";
        String sqlFy = "select t.* from(" + sql+") t";

        sqlFy+= " WHERE Row_Num BETWEEN " + (pageNum-1) * pageSize + " AND " + (pageNum) * pageSize;

        //查询单数据
        Map<String,Object> listCount = jdbcTemplate.queryForMap(sqlCount, new HashMap<>());
        //查询多条数据据
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(sqlFy, new HashMap<>());
        return  Result.success(Long.parseLong(listCount.get("nums").toString()),maps);
    }
}
