package com.fs.company.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.TimeUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.crm.param.CrmCustomerStatisticsParam;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.service.IFsStorePaymentService;
import com.fs.his.service.IFsStoreProductService;
import com.fs.his.service.IFsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/common/index")
public class IndexController
{
    @Autowired
    private IFsUserService userService;
    @Autowired
    private ICrmCustomerService crmCustomerService;
    @Autowired
    private IFsStoreOrderService storeOrderService;

    @Autowired
    private IFsStorePaymentService paymentService;

    @Autowired
    private IFsStoreProductService productService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyUserService comUserService;
    @GetMapping("/getIndexData")
    public R getIndexData(CrmCustomerStatisticsParam param)
    {
        if(StringUtils.isNotEmpty(param.getUserIds())){
            String[] userIds=param.getUserIds().split(",");
            Long[] ids=new Long[userIds.length];
            for(int i=0;i<ids.length; i++){
                ids[i]=Long.parseLong(userIds[i]);
            }
            param.setUsers(ids);
        }
        else{
            //获取所有员工
            CompanyUser usermap=new CompanyUser();
            usermap.setDeptId(param.getDeptId());
            List<CompanyUser> users = comUserService.getUserListByDeptId(usermap);
            List<Long> userIds = users.stream().map(element -> element.getUserId()).collect(Collectors.toList());
            param.setUsers(userIds.toArray(new Long[userIds.size()]));
            if (userIds==null||userIds.size()==0){
                return R.error("此部门无用户");
            }
        }

        Map<String,Object> dataMap =  crmCustomerService.selectIndexData(param);
        return R.ok().put("data",dataMap);
    }
//    @GetMapping("/getIndexData")
//    public R getIndexData()
//    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Map<String,Object> data=new HashMap<>();
//        Long customerCount=crmCustomerService.selectCrmCustomerCount(0,loginUser.getCompany().getCompanyId());
//        Long dayCustomerCount=crmCustomerService.selectCrmCustomerCount(1,loginUser.getCompany().getCompanyId());
//        data.put("customerCount",customerCount);
//        data.put("dayCustomerCount",dayCustomerCount);
//        Long storeOrderCount=storeOrderService.selectFsStoreOrderTotalCount(0,loginUser.getCompany().getCompanyId());
//        Long dayStoreOrderCount=storeOrderService.selectFsStoreOrderTotalCount(1,loginUser.getCompany().getCompanyId());
//        data.put("storeOrderCount",storeOrderCount);
//        data.put("dayStoreOrderCount",dayStoreOrderCount);
//        Long paymentCount=paymentService.selectFsStorePaymentCount(0,loginUser.getCompany().getCompanyId());
//        Long dayPaymentCount=paymentService.selectFsStorePaymentCount(1,loginUser.getCompany().getCompanyId());
//        data.put("paymentCount",paymentCount);
//        data.put("dayPaymentCount",dayPaymentCount);
//
//        return R.ok().put("data",data);
//    }


    @GetMapping("/getStoreOrderData")
    public R getStoreData()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Long[] users=new Long[0];
        TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime("5",null,null);
        timeEntity.setUserIds(users);
        timeEntity.setCompanyId(loginUser.getCompany().getCompanyId());
        Integer cycleNum = timeEntity.getCycleNum();
        Integer beginTime = timeEntity.getBeginTime();
        List<Integer> timeList = new ArrayList<>();
        for (int i = 1; i <= cycleNum; i++) {
            timeList.add(beginTime);
            beginTime = TimeUtils.formatTime(beginTime);
        }
        List<JSONObject> jsonObjectList = storeOrderService.selectFsStoreOrderCounts(timeEntity.toMap());
        List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
        List<Integer> orderCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("orderCount")).collect(Collectors.toList());
        List<Integer> payPrice = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("payPrice")).collect(Collectors.toList());
        return R.ok().put("dates",dates).put("orderCount",orderCount).put("payPrice",payPrice);
    }
    @GetMapping("/getStorePaymentData")
    public R getStorePaymentData()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Long[] users=new Long[0];
        TimeUtils.TimeEntity timeEntity=TimeUtils.parseTime("5",null,null);
        timeEntity.setUserIds(users);
        timeEntity.setCompanyId(loginUser.getCompany().getCompanyId());
        Integer cycleNum = timeEntity.getCycleNum();
        Integer beginTime = timeEntity.getBeginTime();
        List<Integer> timeList = new ArrayList<>();
        for (int i = 1; i <= cycleNum; i++) {
            timeList.add(beginTime);
            beginTime = TimeUtils.formatTime(beginTime);
        }
        List<JSONObject> jsonObjectList = paymentService.selectFsStorePaymentCounts(timeEntity.toMap());
        List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
        List<Integer> orderCount = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("orderCount")).collect(Collectors.toList());
        List<Integer> payMoney = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("payMoney")).collect(Collectors.toList());
        return R.ok().put("dates",dates).put("orderCount",orderCount).put("payMoney",payMoney);
    }

}
