package com.fs.company.controller.handwrite;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.framework.service.TokenService;
import com.fs.his.param.CollectionOrcParam;
import com.fs.his.vo.CollectionOrcVO;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.handwrite.domain.HandwriteCollection;
import com.fs.handwrite.service.IHandwriteCollectionService;

/**
 * 手写信息采集表Controller
 * 
 * @author fs
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/handwrite/collection")
public class HandwriteCollectionController extends BaseController
{
    @Autowired
    private IHandwriteCollectionService handwriteCollectionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询手写信息采集表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HandwriteCollection handwriteCollection)
    {
        startPage();
        List<HandwriteCollection> list = handwriteCollectionService.selectHandwriteCollectionList(handwriteCollection);
        for (HandwriteCollection item : list) {
            if (item.getPatientPhone() != null) {
                item.setPatientPhone(item.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出手写信息采集表列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody HandwriteCollection handwriteCollection) throws IOException {
        List<HandwriteCollection> list = handwriteCollectionService.selectHandwriteCollectionList(handwriteCollection);
        for (HandwriteCollection item : list) {
            if (item.getPatientPhone() != null) {
                item.setPatientPhone(item.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        ExcelUtil<HandwriteCollection> util = new ExcelUtil<HandwriteCollection>(HandwriteCollection.class);
        util.exportExcel(response, list, "手写信息采集表数据");
    }

    /**
     * 获取手写信息采集表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        HandwriteCollection handwriteCollection = handwriteCollectionService.selectHandwriteCollectionById(id);
        if (handwriteCollection.getPatientPhone() != null) {
            handwriteCollection.setPatientPhone(handwriteCollection.getPatientPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        return AjaxResult.success(handwriteCollection);
    }

    /**
     * 新增手写信息采集表
     */
    @PostMapping
    public AjaxResult add(@RequestBody HandwriteCollection handwriteCollection)
    {
        //获取当前登录用户
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        handwriteCollection.setCompanyUserId(currentUserId);
        return toAjax(handwriteCollectionService.addCollectionAndUpdateOrderStatus(handwriteCollection));
    }

    /**
     * 新增手写信息采集并更新商城订单关联的手写信息采集id
     */
    @PostMapping("/addCollectionAndUpdateOrderStatus")
    public AjaxResult addCollectionAndUpdateOrderStatus(@RequestBody HandwriteCollection handwriteCollection)
    {
        //获取当前登录用户
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        handwriteCollection.setCompanyUserId(currentUserId);
        return toAjax(handwriteCollectionService.addCollectionAndUpdateOrderStatus(handwriteCollection));
    }

    /**
     * 修改手写信息采集表
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HandwriteCollection handwriteCollection)
    {
        //获取当前登录用户
        Long currentUserId = SecurityUtils.getLoginUser().getUser().getUserId();
        handwriteCollection.setUpdateBy(currentUserId);
        return toAjax(handwriteCollectionService.updateHandwriteCollection(handwriteCollection));
    }

    /**
     * 删除手写信息采集表
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(handwriteCollectionService.deleteHandwriteCollectionByIds(ids));
    }

    /**
     * 根据商城订单号查询订单信息
     * */
    @GetMapping("/getOrderCodeInfo/{orderCode}")
    public AjaxResult getOrderCodeInfo(@PathVariable String orderCode) {
        if (StringUtils.isBlank(orderCode)){
            return AjaxResult.error("订单号不能为空");
        }
        FsStoreOrderScrm orderCodeInfo = handwriteCollectionService.getOrderCodeInfo(orderCode);
        if (orderCodeInfo == null){
            return AjaxResult.error("商城订单不存在");
        }
        return AjaxResult.success(orderCodeInfo);
    }

    /**
     * 检测上传的订单号
     * */
    @GetMapping("/checkOrderCode/{orderCode}")
    public AjaxResult checkOrderCode(@PathVariable String orderCode) {
        if (StringUtils.isBlank(orderCode)){
            return AjaxResult.error("订单号不能为空");
        }
        return handwriteCollectionService.checkOrderCode(orderCode);
    }

    @PostMapping("/orc")
    public AjaxResult imageProcess(@RequestBody CollectionOrcParam param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        CollectionOrcVO vo = handwriteCollectionService.imageProcess(param);
        return AjaxResult.success(vo);
    }
}