package com.fs.his.controller;

import cn.hutool.json.JSONUtil;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyMoneyLogs;
import com.fs.company.mapper.CompanyMoneyLogsMapper;
import com.fs.company.service.ICompanyService;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.his.domain.*;
import com.fs.his.dto.DataDTO;
import com.fs.his.dto.inquiryOrderChartDTO;
import com.fs.his.mapper.FsInquiryOrderMapper;
import com.fs.his.mapper.FsPrescribeMapper;
import com.fs.his.mapper.FsStoreOrderMapper;
import com.fs.his.param.FsInquiryOrderFinishParam;
import com.fs.his.service.*;
import com.fs.his.vo.*;
import com.fs.im.dto.*;
import com.fs.im.service.IImService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/his/data")
public class DataController extends BaseController {
    @Autowired
    IFsDataService iFsDataService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    FsStoreOrderMapper fsStoreOrderMapper;
    @Autowired
    private CompanyMoneyLogsMapper moneyLogsMapper;
    @Autowired
    private IFsInquiryOrderService iFsInquiryOrderService;
    @Autowired
    IFsStoreSubOrderService fsStoreSubOrderService;
    @Autowired
    RedisCache redisCache;

    @Autowired
    IFsPrescribeService fsPrescribeService;

    @GetMapping("/orderData")
    public AjaxResult list() {
        OrderDataVO list = iFsDataService.getOrderData();
        return AjaxResult.success(list);
    }
    @GetMapping("/testSendSub/{id}")
    public AjaxResult testSendSub(@PathVariable Long id) {
        FsSubOrderResultVO fsSubOrderResultVO = fsStoreSubOrderService.TuiFsStoreSubOrderByStoreOrder(id);
        return AjaxResult.success(fsSubOrderResultVO);
    }


    @GetMapping("/test/{id}")
    public AjaxResult test2() {
        String orderCode =  OrderCodeUtils.getOrderSn();
        System.out.println(orderCode);
        if(com.fs.common.utils.StringUtils.isEmpty(orderCode)) {
            System.out.println("失败");
        }

        return AjaxResult.success(1111111);
    }
    @Autowired
    private IImService imService;
    @Autowired
    private IFsFollowService fsFollowService;
    @GetMapping("/endFollow/{id}")
    public AjaxResult endFollow(@PathVariable Long id) {
        FsFollow follow=fsFollowService.selectFsFollowByFollowId(id);
        //发送给用户
        MsgDTO msgDTO=new MsgDTO();
        MsgCustomDTO customDTO=new MsgCustomDTO();
        customDTO.setType("finishDrugReport");
        customDTO.setImType(0);
        customDTO.setOrderId(follow.getStoreOrderId().toString());
        customDTO.setFollowId(follow.getFollowId().toString());
        msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
        msgDTO.setFrom_Account("D-"+follow.getDoctorId());
        msgDTO.setTo_Account("U-"+follow.getUserId());
        List<MsgDataDTO> msgs=new ArrayList<>();
        MsgDataDTO msg=new MsgDataDTO();
        msg.setMsgType("TIMTextElem");
        msg.setMsgContent(new MsgDataFormatDTO("咨询已结束"));
        msgs.add(msg);
        msgDTO.setMsgBody(msgs);
        imService.sendMsg(msgDTO);
        return AjaxResult.success();
    }
    @GetMapping("/Follow/{id}")
    public AjaxResult Follow(@PathVariable Long id) {
        FsFollow follow=fsFollowService.selectFsFollowByFollowId(id);
        //发送给用户
        MsgDTO msgDTO=new MsgDTO();
        MsgCustomDTO customDTO=new MsgCustomDTO();
        customDTO.setType("startDrugReport");
        customDTO.setImType(2);
        customDTO.setOrderId(follow.getStoreOrderId().toString());
        customDTO.setFollowId(follow.getFollowId().toString());
        msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
        msgDTO.setFrom_Account("D-"+follow.getDoctorId());
        msgDTO.setTo_Account("U-"+follow.getUserId());
        List<MsgDataDTO> msgs=new ArrayList<>();
        MsgDataDTO msg=new MsgDataDTO();
        msg.setMsgType("TIMTextElem");
        msg.setMsgContent(new MsgDataFormatDTO("您好，我是您的专属药师，您有任何问题都可以先留言给我，请耐心等待一下，用户咨询量较多，我会按咨询顺序一一回复哒~"));
        msgs.add(msg);
        msgDTO.setMsgBody(msgs);
        imService.sendMsg(msgDTO);
        return AjaxResult.success();
    }


    @GetMapping("/end/{id}")
    public AjaxResult end(@PathVariable Long id) {
        FsInquiryOrder order = iFsInquiryOrderService.selectFsInquiryOrderByOrderId(id);
        //发送给用户
        MsgDTO msgDTO=new MsgDTO();
        MsgCustomDTO customDTO=new MsgCustomDTO();
        customDTO.setType("finishInquiry");
        customDTO.setOrderId(order.getOrderId().toString());
        customDTO.setImType(0);
        msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
        msgDTO.setFrom_Account("D-"+order.getDoctorId());
        msgDTO.setTo_Account("U-"+order.getUserId());
        List<MsgDataDTO> msgs=new ArrayList<>();
        MsgDataDTO msg=new MsgDataDTO();
        msg.setMsgType("TIMCustomElem");
        InquiryOrderMsgDTO inquiryOrderMsgDTO=new InquiryOrderMsgDTO();
        inquiryOrderMsgDTO.setTitle("问诊结束");
        String ext= JSONUtil.toJsonStr(inquiryOrderMsgDTO);
        msg.setMsgContent(new MsgDataFormatDTO("finishInquiry",ext,order.getOrderId().toString()));
        msgs.add(msg);
        msgDTO.setMsgBody(msgs);
        imService.sendMsg(msgDTO);
        return AjaxResult.success();
    }
    @GetMapping("/addCom/{code}")
    public AjaxResult addCompanyMoney(@PathVariable String code){
        FsStoreOrder order = fsStoreOrderMapper.selectFsStoreOrderByOrderCode(code);
            CompanyMoneyLogs moneyLog1 = moneyLogsMapper.selectCompanyMoneyLogsByOrderId(order.getOrderId(), 5);
            CompanyMoneyLogs moneyLog2 = moneyLogsMapper.selectCompanyMoneyLogsByOrderId(order.getOrderId(),3);
            if (moneyLog1==null){
                companyService.subtractCompanyMoney(order);
            }
            if (moneyLog2==null){
                companyService.addCompanyMoney(order);
                FsStoreOrder orderMap = new FsStoreOrder();
                orderMap.setOrderId(order.getOrderId());
                orderMap.setTuiMoneyTime(new Date());
                fsStoreOrderMapper.updateFsStoreOrder(orderMap);
            }
        return AjaxResult.success();
    }

    @GetMapping("/addCF/{code}")
    public AjaxResult addCF(@PathVariable Long code){

        String s = fsPrescribeService.PrescribeImg(code);
        return AjaxResult.success();
    }


    @GetMapping("/orderChartData")
    public AjaxResult orderChartList() {
        List<ChartDataVO> list = iFsDataService.getOrderChartData();
        return AjaxResult.success(list);
    }

    @GetMapping("/inquiryOrderChartData")
    public AjaxResult inquiryOrderChartList() {
        List<ChartDataVO> list = iFsDataService.getInquiryOrderChartData();
        return AjaxResult.success(list);
    }
    @GetMapping("/doctorChartData/{type}")
    public AjaxResult doctorChartData(@PathVariable("type")Integer type) {
        List<DoctorChartVO> list = iFsDataService.doctorChartData(type);
        return AjaxResult.success(list);
    }


    @GetMapping("/storeOrderChart")
    public AjaxResult storeOrderChart( DataDTO dto) {
        List<ChartDataVO> list = iFsDataService.storeOrderChart(dto);
        return AjaxResult.success(list);
    }

    @GetMapping("/storeOrderChartStore")
    public AjaxResult storeOrderChartStore( DataDTO dto) {
        List<ChartDaVO> list = iFsDataService.storeOrderChartStore(dto);
        return AjaxResult.success(list);
    }

    @GetMapping("/inquiryOrderChart")
    public AjaxResult inquiryOrderChart( inquiryOrderChartDTO dto) {
        List<ChartDataVO> list = iFsDataService.inquiryOrderChart(dto);
        return AjaxResult.success(list);
    }

    @GetMapping("/inquiryOrderChartInq")
    public AjaxResult inquiryOrderChartInq( inquiryOrderChartDTO dto) {
        List<ChartDaVO> list = iFsDataService.inquiryOrderChartInq(dto);
        return AjaxResult.success(list);
    }

    @Log(title = "药品订单统计", businessType = BusinessType.EXPORT)
    @GetMapping("/storeExport")
    public AjaxResult storeExport( DataDTO dto)
    {
        List<ChartDaVO> list = iFsDataService.storeOrderChartStore(dto);
        ExcelUtil<ChartDaVO> util = new ExcelUtil<ChartDaVO>(ChartDaVO.class);
        return util.exportExcel(list, "药品订单统计");
    }

    @Log(title = "问诊统计", businessType = BusinessType.EXPORT)
    @GetMapping("/inquiryExport")
    public AjaxResult inquiryExport( inquiryOrderChartDTO dto)
    {
        List<ChartDaVO> list = iFsDataService.inquiryOrderChartInq(dto);
        ExcelUtil<ChartDaVO> util = new ExcelUtil<ChartDaVO>(ChartDaVO.class);
        return util.exportExcel(list, "问诊统计");
    }
}
