package com.huigou.topsun.proofing.controller;

import com.huigou.context.Operator;
import com.huigou.context.OrgUnit;
import com.huigou.topsun.product.application.ProductLookedApplication;
import com.huigou.topsun.product.domain.ProductLooked;
import com.huigou.topsun.proofing.application.ProofingApplyApplication;
import com.huigou.topsun.proofing.domain.model.ProofingApply;

import com.huigou.topsun.proofing.domain.vo.ProofingApplyAndLookedVo;
import com.huigou.topsun.technology.application.TechnologyApplication;
import com.huigou.topsun.technology.application.TechnologyProcessApplication;
import com.huigou.topsun.technology.domain.model.Technology;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.bmp.common.BizBillStatus;
import com.huigou.uasp.client.CommonController;
import com.huigou.uasp.log.annotation.LogInfo;
import com.huigou.uasp.log.domain.model.LogType;
import com.huigou.uasp.log.domain.model.OperationType;
import com.huigou.util.SDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

@Controller
@ControllerMapping("/proofingApply")
public class ProofingApplyController extends CommonController {
    protected String getPagePath() {
        return "/biz/topsun/proofing/";
    }

    @Autowired
    ProofingApplyApplication proofingApplyApplication;
    @Autowired
    TechnologyApplication technologyApplication;
    @Autowired
    ProductLookedApplication productLookedApplication;

    @Resource
    private TechnologyProcessApplication technologyProcessApplication;

    public String slicedTechnologyProcessList(){
        SDO sdo = this.getSDO();
        String technologyId = sdo.getString("technologyId");
        return packGridDataAndResult(technologyProcessApplication.slicedTechnologyProcessList(technologyId));
    }

        public String slicedProofingApplyList(){
            List<ProofingApply> proofingApplyList = proofingApplyApplication.getProofingApplyList();
            Map<String,Object> map =new HashMap();
            map.put("Rows",proofingApplyList);
            return toResult(map);
    }

    public String forwardProofingApplyList(){
        return forward("proofingApplyList");
    }


    /**
     * 打样表单页
     * @return
     */
    public String forwardProofingApply(){

        this.putAttribute("processDefinitionKey",ProofingApplyApplication.PROCESS_DEFINITION_KEY);
        String applicationNo= UUID.randomUUID().toString().replace("-", "");
        this.putAttribute("procUnitId", "Apply");

        ProofingApply proofingApply = new ProofingApply();
        proofingApply.setProofingApplyNo(applicationNo);
        proofingApply.setStatusId(BizBillStatus.APPLYING.getId());
        Operator operator = getOperator();
        proofingApply.setDefaultValues(new OrgUnit(operator.getFullId(), operator.getFullName()));
        return forward("proofingApply",proofingApply);
    }

    public String slicedTechnologyList(){
        SDO sdo = this.getSDO();
        String productId = sdo.getString("productId");
        List<Technology> allByProduct= technologyApplication.findAllByProductId(productId);
        Map<String,Object> map =new HashMap();
        map.put("Rows",allByProduct);
        return toResult(map);
    }




    /**
     * 工作流审核跳转页面
     * @return
     */
    public String showProofingApply() {
        SDO sdo = this.getSDO();
        String id = sdo.getBizId();
        ProofingApplyAndLookedVo proofingApplyAndLookedVo = proofingApplyApplication.getProofingApplyAndLookedVoByPAId(id);
        return forward("proofingApply", proofingApplyAndLookedVo);
    }

    public String queryProductLooked(){
        SDO sdo = this.getSDO();
        String productId = sdo.getString("productId");
        ProductLooked productLooked = new ProductLooked();
        productLooked.setProductId(productId);
        ProductLooked newProductLooked = productLookedApplication.getProductLookedByEntity(productLooked);
        return toResult(newProductLooked);
    }
}
